创建一个Animal类,私有成员变量name(String),age(int )和公共成员方法:infPrint()(输出成员变量name,age,通过调用toString()实现)、shout()(发出叫声);
1) 创建一个Dog类,通过extends关键字继承Animal类,有私有成员color(String),及其setter和getter方法;重写shout()(发出汪汪叫声),重写infPrint();
2) 在主方法中使用new 创建一个Dog对象dog1,并通过setter方法颜色为黑色 ;调用infPrint()
3) 在主方法中通过dog1调用shout().
4) 在主方法中使用new 创建一个Dog对象dog2,通过setter方法给三个成员变量赋值:金毛犬 2,黄色,调用infPrint()
class Animal{
private String name;
private int age;
public void shout() {
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Animal [name=" + name + ", age=" + age;
}
public void infPrint() {
System.out.println(this.toString());
}
}
class Dog extends Animal{
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void shout() {
System.out.println("汪~汪呜!");
}
@Override
public String toString() {
return ",color=" + color + "]";
}
@Override
public void infPrint() {
System.out.println(super.toString()+this.toString());
}
}
public class Answer7731291 {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.setColor("黑色");
dog1.infPrint();
dog1.shout();
Dog dog2=new Dog();
dog2.setName("金毛犬");
dog2.setAge(2);
dog2.setColor("黄色");
dog2.infPrint();
}
}
参考一下,差不多类似的
public class Animal {
private String name;
private int age;
public Animal(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 outPut(){
System.out.println("name:"+this.getName());
System.out.println("age:"+this.getAge());
}
}
public interface Eattable {
void beatted();
}
package com.animaltest;
public class Dog extends Animal{
private String wing;
private String flyDistance;
public Bird(String name, int age) {
super(name, age);
this.wing = wing;
this.flyDistance = flyDistance;
}
public String getWing() {
return wing;
}
public void setWing(String wing) {
this.wing = wing;
}
public String getFlyDistance() {
return flyDistance;
}
public void setFlyDistance(String flyDistance) {
this.flyDistance = flyDistance;
}
@Override
public void outPut() {
super.outPut();
System.out.println("wing:"+this.getWing());
System.out.println("flyDistance:"+this.getFlyDistance());
}
}
public class Beast extends Animal implements Eattable{
private String localPlace;
private String food;
public Beast(String name, int age) {
super(name, age);
this.localPlace = localPlace;
this.food = food;
}
public String getLocalPlace() {
return localPlace;
}
public void setLocalPlace(String localPlace) {
this.localPlace = localPlace;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
@Override
public void beatted() {
System.out.println();
}
@Override
public void outPut() {
super.outPut();
System.out.println("localPlace:"+this.getLocalPlace());
System.out.println("food:"+this.getFood());
}
}
Java 对象和类 https://www.runoob.com/java/java-object-classes.html
Java的封装与继承 https://blog.csdn.net/kingJamesbond/article/details/120174591
照着练就行了