/**
具体类 : 对现实世界事物的抽象定义
抽象类 : 对某一类型的不同种事物的抽象定义
*/
//编写抽象类Pet, 属性:名字,年龄,体重; 抽象方法speak,eat
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;
}
// 抽象方法 : 只有方法签名没有方法体, 被abstract修饰
public abstract void speak();//抽象方法
public abstract void eat();//抽象方法
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ",体重:" + weight + "Kg";
}
}
//编写Bird类继承Pet,特有属性:flySpeed
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;
}
// 方法实现!! 去掉abstract 加上方法体
// 具体子类重写并加上方法体抽象父类的抽象方法
@Override
public void speak() {
System.out.println("叽叽喳喳的叫个不停!!!!");
}
@Override
public void eat() {
System.out.println("最喜欢吃虫子!!!!正在吃虫子中......");
}
@Override
public String toString() {//父类中toString方法重写
return super.toString() + ",飞行速度:" + flySpeed + "Km/小时!!!";
}
}
//编写Cat类继承Pet,特有属性:颜色
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() {//父类中toString方法重写
return super.toString() + ",颜色:" + color;
}
}
//编写Dog类继承Pet,特有属性:品种
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() {//父类中toString方法重写
return super.toString() + ",品种:" + variety;
}
}
/*
创建6个元素的数组,分别存放2个Cat、2个Dog和2个Bird对象
将数组元素按重量倒序排序,然后打印各对象的字符串表示(toString)
*/
class PetTest {
public static void main(String[] args) {
//创建6个元素的数组,分别存放2个Cat、2个Dog和2个Bird对象
Pet[] ps = new Pet[6];
//public Cat(String name,int age,double weight,String color)
ps[0] = new Cat("小咪",2,2.5,"黑色");
ps[1] = new Cat("二咪",3,3.5,"白色");
//public Dog(String name,int age,double weight,String variety)
ps[2] = new Dog("大汪",3,5.5,"泰迪");
ps[3] = new Dog("二汪",2,4.5,"哈巴");
//public Bird(String name,int age,double weight,int flySpeed)
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);
//p.speak();
//p.eat();
}
}
}
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());
}
}
java学习之路 之 高级类特性-抽象类-练习题
最新推荐文章于 2024-12-10 20:14:41 发布