现定义一个类体系,基类为Dog,派生类为斑点狗SpottedDog类和非斑点狗UnspottedDog类,具体要求如下:
(1)在基类中记录狗的品种breed,体重weight以及颜色color等属性,定义一个方法show()显示Dog信息;
(2)在UnspottedDog类中,调用Dog类的构造方法,重写show()方法,只显示狗的品种;
(3)在SpottedDog类中,新增表示斑点颜色的spotColor属性,定义包含四个属性的构造方法,重写show()方法;
(4)定义测试类,构造斑点狗对象,分别显示斑点狗的品种、体重、颜色和品种、颜色、斑点颜色;构造非斑点狗对象,显示狗的品种、体重、颜色信息。
(说明:构造斑点狗对象和非斑点狗对象时要分别输入,各属性值之间用空格分割,输入完后按回车键确认,输入内容参照测试数据。)
package com.fm.demo;
//狗类
public class Dog {
public String breed;
public int weight;
public String color;
public Dog() {
}
public Dog(String breed, int weight, String color) {
this.breed = breed;
this.weight = weight;
this.color = color;
}
public void show() {
System.out.println("这是一只" + this.breed +","+ "体重" + this.weight +"斤,"+ "颜色" + this.color);
}
}
//spottedDog类
class spottedDog extends Dog {
String spotColor;
public spottedDog(String breed, int weight, String color, String spotColor) {
super(breed, weight, color);
this.spotColor = spotColor;
}
public void show() {
System.out.println("这是一只"+this.breed+","+"颜色为"+this.color+","+"斑点颜色为"+this.spotColor);
}
}
//UnspottedDog 类
class UnspottedDog extends Dog {
public UnspottedDog(String breed, int weight, String color) {
super(breed, weight, color);
}
public void show() {
System.out.println("这只狗的品种" + this.breed);
}
}
//测试类
package com.fm.demo;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int weight;
String bread;
String color;
String spotColor;
bread = sc.next();
weight = sc.nextInt();
color = sc.next();
spotColor = sc.next();
Dog dog = new Dog(bread, weight, color);
dog.show();
spottedDog spottedDog = new spottedDog(bread, weight, color, spotColor);
spottedDog.show();
UnspottedDog unspottedDog = new UnspottedDog(sc.next(),sc.nextInt(),sc.next());
unspottedDog.show();
}
}