public class Animal {
//动物的属性
private String name; //名字
private int age; //年龄
private String color; //颜色
//调用它们的get()、set()方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
//创建一个shout()方法
public void shout(){
}
}
//Cat类 继承 Animal类
public class Cat extends Animal{
public void shout(){
System.out.println("一只"+this.getColor()+this.getName()+"喵喵叫");
}
}
//Dog类 继承 Animal类
public class Dog extends Animal{
public void shout(){
System.out.println("一只"+this.getColor()+this.getName()+"汪汪叫");
}
}
//测试类
public abstract class TestDemo {
public static void main(String[] args) {
//动物
Animal a1=new Dog(); //创建一个小狗对象
a1.setColor("黄色的"); //它的颜色
a1.setName("小白"); //名字
a1.shout(); //叫
Animal a2=new Cat(); //创建一个小猫对象
a2.setColor("粉色的"); //它的颜色
a2.setName("小花"); //名字
a2.shout(); //叫
//手机
Mobile m1=new Samsung(); //创建一个三星手机
m1.setBrand("三星"); //品牌
m1.setColor("星耀黑"); //颜色
m1.setModel("Not10"); //型号
m1.show(); //展示
Mobile m2=new Apple(); //创建一个苹果手机
m1.setBrand("苹果"); //品牌
m1.setColor("玫瑰金"); //颜色
m1.setModel("Plus6"); //型号
m1.show(); //展示
}
}