i汽车类
package com.lanxi.demo2_3;
public class Car {
private String name;
private int price;
@Override//重写toString方法
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
}
public Car() {//创建无参构造方法
super();
}
public Car(String name, int price) {//构造有参构造方法
super();
this.name = name; this.price = price;
}
//创建get,set方法
public String getName() {
return name;
}
public int getPrice() {
return price;
}
}
测试类
package com.lanxi.demo2_3;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Set set=new HashSet();
Car car1=new Car("奔驰",26300);
Car car2=new Car("宝马",21300);
Car car3=new Car("东风",23500);
Car car4=new Car("奥迪",23400);
Car car5=new Car("奔驰",26300);
set.add(car1);
set.add(car2);
set.add(car3);
set.add(car4);
set.add(car5);
Iterator it=set.iterator();
while(it.hasNext()){
Car car=(Car) it.next();
System.out.println(car);
System.out.println("降价后汽车价钱:"+(car.getPrice()-10000));
}
}
}