/**
具体类 : 对现实世界事物的抽象定义
抽象类 : 对某一类型的不同种事物的抽象定义
*/
public abstract class Pet {
private String name;
private int age;
private double weight;
public Pet() {}
public Pet(String name,int age,double weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getWeight() {
return weight;
}
public abstract void speak();
public abstract void eat();
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ",体重:" + weight + "Kg";
}
}
public class Bird extends Pet {
private int flySpeed;
public Bird() {}
public Bird(String name,int age,double weight,int flySpeed) {
super(name,age,weight);
this.flySpeed = flySpeed;
}
public void setFlySpeed(int flySpeed) {
this.flySpeed = flySpeed;
}
public int getFlySpeed() {
return flySpeed;
}
@Override
public void speak() {
System.out.println("叽叽喳喳的叫个不停!!!!");
}
@Override
public void eat() {
System.out.println("最喜欢吃虫子!!!!正在吃虫子中......");
}
@Override
public String toString() {
return super.toString() + ",飞行速度:" + flySpeed + "Km/小时!!!";
}
}
public class Cat extends Pet {
private String color;
public Cat() {}
public Cat(String name,int age,double weight,String color) {
super(name,age,weight);
this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
@Override
public void speak() {
System.out.println("喵喵喵的叫个不停!!!!");
}
@Override
public void eat() {
System.out.println("最喜欢鱼!!!!正在吃鱼中......");
}
@Override
public String toString() {
return super.toString() + ",颜色:" + color;
}
}
public class Dog extends Pet {
private String variety;
public Dog() {}
public Dog(String name,int age,double weight,String variety) {
super(name,age,weight);
this.variety = variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public String getVariety() {
return variety;
}
@Override
public void speak() {
System.out.println("汪汪汪的叫个不停!!!!");
}
@Override
public void eat() {
System.out.println("最喜欢骨头!!!!正在吃骨头中......");
}
@Override
public String toString() {
return super.toString() + ",品种:" + variety;
}
}
class PetTest {
public static void main(String[] args) {
Pet[] ps = new Pet[6];
ps[0] = new Cat("小咪",2,2.5,"黑色");
ps[1] = new Cat("二咪",3,3.5,"白色");
ps[2] = new Dog("大汪",3,5.5,"泰迪");
ps[3] = new Dog("二汪",2,4.5,"哈巴");
ps[4] = new Bird("小叼",2,1.5,30);
ps[5] = new Bird("大叼",3,3.0,40);
for (Pet p : ps) {
System.out.println(p);
}
System.out.println("-----------------------排序后------------------");
for (int i = 0;i < ps.length - 1;i++) {
for (int j = 0;j < ps.length - 1 - i;j++){
if (ps[j].getWeight() < ps[j + 1].getWeight()) {
Pet tmp = ps[j];
ps[j] = ps[j + 1];
ps[j + 1] = tmp;
}
}
}
for (Pet p : ps) {
System.out.println(p);
}
}
}
public abstract class Frock {
private int size;
private String color;
private double price;
public Frock() {}
public Frock(int size, String color, double price{
this.size = size;
this.color = color;
this.price = price;
}
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "尺寸:" + size + ",颜色:" + color + ",价格:" + price;
}
public abstract double calcArea();
}
class Shirt extends Frock {
public Shirt() {}
public Shirt(int size, String color, double price{
super(size, color, price);
}
@Override
public double calcArea() {
return getSize() * 1.3;
}
}
class FrockTest {
public static void main(String[] args) {
Frock f = new Shirt(180, "白色", 200);
System.out.println(f);
System.out.println(f.calcArea());
Frock f2 = new Shirt();
System.out.println(f2);
System.out.println(f2.calcArea());
}
}