重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
【思路】
定义Dog类
属性:
年龄(age)、颜色(color)
行为:
eat(String something)(something表示吃的东西)
看家lookHome方法(无参数)
定义Cat类
属性:
年龄(age)、颜色(color)
行为;
eat(String something)(something代表吃的东西)
逮老鼠catchMouse方法(无参数)
定义Person类
属性:
姓名(name)、年龄(age)
行为:
keepPet(Dog dog,String something)方法
功能:喂养宠物狗,something表示喂养的东西
行为:
keepPet(Cat cat,String something)方法
功能:喂养宠物猫,something表示喂养的东西
生成空参有参构造,set和get方法
定义测试类(完成以下打印效果)
属性:
keepPet(Dog dog,String something)方法打印内容如下:
年龄为30岁的老王养了一只黑色的3岁的狗
3岁的黑色的狗两只前脚死死地抱住骨头猛吃
keepPet(Cat cat,String something)方法打印内容如下:
年龄为30岁的老王养了一只白色的2岁的猫
2岁的白色的猫眯着眼睛侧着头吃鱼
思考:
- Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet方法,过于繁琐,能否简化,并体会简化的好处?
- Dog和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中调用特有方法
第一步,根据Dog类和Cat类的相同的属性和行为创建他们的父类Animal类,Animal类里应该要写好共有的属性并且要进行封装,接着还要写好eat行为。写好父类后要写Dog类和Cat类。这两个类在继承Animal类的时候要注意加关键字extends。并且还要对eat行为进行重写,此时eat行为的重写要注意输出时候的格式类型
//父类
public class Animal {private int age;
private String color;
public Animal() {}
public Animal(int age, String color) {this.age = age;
this.color = color;
}
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;
}
public void eat(String something) {System.out.println("动物在吃" + something);
}
}
//Dog类
public class Dog extends Animal {public Dog() {}
public Dog(int age, String color) {super(age, color);
}
@Override
public void eat(String something) {System.out.println(getAge() + "岁的" + getColor() + "色的狗两只前脚死死地抱住" + something + "猛吃");
}
public void lookhome() {System.out.println("狗在看家");
}
}
//Cat类
public class Cat extends Animal {public Cat() {}
public Cat(int age, String color) {super(age, color);
}
@Override
public void eat(String something) {System.out.println(getAge() + "岁的" + getColor() + "色的猫眯着眼睛侧着头吃着" + something);
}
public void catchMouse() {System.out.println("猫抓老鼠");
}
}
注意Dog类和Cat类主要的成员变量已经在父类中定义过了,可以直接调用,但是要注意有superl来说明来自父类
第二步,根绝要求创建Person类。Person类的属性就是name、age,行为就keepPet。但是keepPet主要是针对两个类的,所以要进行重载。
//Person类
public class Person {private String name;
private int age;
public Person() {}
public Person(String name, int age) {this.name = name;
this.age = age;
}
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 void keepPet(Dog dog, String something) {System.out.println("年龄为" + getAge() + "岁的" + getName() + "养了一只" + dog.getColor() + "的" + dog.getAge() + "岁的狗");
dog.eat(something);
}
public void keepPet(Cat cat, String something) {System.out.println("年龄为" + getAge() + "岁的" + getName() + "养了一只" + cat.getColor() + "的" + cat.getAge() + "岁的猫");
cat.eat(something);
}
}
第三步,在主函数中创建初始化Dog、Cat、Person,最后直接调用Person下的方法,进行输出
public class Main {public static void main(String[] args) {Person p = new Person("老王", 20);
Dog dog = new Dog(1, "黑");
Cat cat = new Cat(2, "白");
p.keepPet(dog, "骨头");
p.keepPet(cat, "鱼");
}
}
【思考】Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet方法,过于繁琐,能否简化,并体会简化的好处?
Dog和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中调用特有方法
当子类过多的时候,我们一个一个的定义胡话会过于麻烦,不仅如此,还要写很多行代码。为了处理这种情况,我们可以进行一些转化。
当子类过多的时候,我们可以直接用他们的父类作为形参对keepPet进行进行一些修改。
public class Person {private String name;
private int age;
public Person() {}
public Person(String name, int age) {this.name = name;
this.age = age;
}
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 void keepPet(Animal a,String something){System.out.println("年龄为" + getAge() + "岁的" + getName() + "养了一只" + a.getColor() + "的" + a.getAge() + "岁的动物");
a.eat(something);
}
}
注意此时的代码有几个弊端:1. 输出的时候没法说明在这里进行说明是什么动物,只能用“动物”来代替父类中的所有子类;2. 不能调用各个子类的特有的方法,只能调用在父类中已经写好的方法;
面对上述问题可以进行强转,将进入的类型强转成需要的类型,这里主要是用到了intanceof
public class Person {private String name;
private int age;
public Person() {}
public Person(String name, int age) {this.name = name;
this.age = age;
}
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 void keepPet(Animal a, String something) {if (a instanceof Dog d) {System.out.println("年龄为" + getAge() + "岁的" + getName() + "养了一只" + a.getColor() + "的" + a.getAge() + "岁的狗");
d.eat(something);
} else if (a instanceof Cat c) {System.out.println("年龄为" + getAge() + "岁的" + getName() + "养了一只" + c.getColor() + "的" + c.getAge() + "岁的猫");
c.eat(something);
}else{System.out.println("没有这种动物");
}
}
}
注意利用intanceof将父类转化成子类之前,要确定这俩确定是继承关系
【总代吗】//父类
public class Animal {private int age;
private String color;
public Animal() {}
public Animal(int age, String color) {this.age = age;
this.color = color;
}
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;
}
public void eat(String something) {System.out.println("动物在吃" + something);
}
}
//Dog类
public class Dog extends Animal {public Dog() {}
public Dog(int age, String color) {super(age, color);
}
@Override
public void eat(String something) {System.out.println(getAge() + "岁的" + getColor() + "色的狗两只前脚死死地抱住" + something + "猛吃");
}
public void lookhome() {System.out.println("狗在看家");
}
}
//Cat类
public class Cat extends Animal {public Cat() {}
public Cat(int age, String color) {super(age, color);
}
@Override
public void eat(String something) {System.out.println(getAge() + "岁的" + getColor() + "色的猫眯着眼睛侧着头吃着" + something);
}
public void catchMouse() {System.out.println("猫抓老鼠");
}
}
//Person类
public class Person {private String name;
private int age;
public Person() {}
public Person(String name, int age) {this.name = name;
this.age = age;
}
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 void keepPet(Animal a, String something) {if (a instanceof Dog d) {System.out.println("年龄为" + getAge() + "岁的" + getName() + "养了一只" + a.getColor() + "的" + a.getAge() + "岁的狗");
d.eat(something);
} else if (a instanceof Cat c) {System.out.println("年龄为" + getAge() + "岁的" + getName() + "养了一只" + c.getColor() + "的" + c.getAge() + "岁的猫");
c.eat(something);
}else{System.out.println("没有这种动物");
}
}
}
//测试类
public class Main {public static void main(String[] args) {Person p = new Person("老王", 20);
Dog dog = new Dog(1, "黑");
Cat cat = new Cat(2, "白");
p.keepPet(dog, "骨头");
p.keepPet(cat, "鱼");
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧