生成主目录
7173
分析 运用到了子类与父类的继承关系,子类使用父类的构造方法无聊又麻烦的题
import java.util.*;
class Dog{
String breed;
int weight;
String color;
Dog(String breed,int weight,String color){
this.breed=breed;
this.weight=weight;
this.color=color;
}
void show(){
System.out.println("这是一只"+breed+"体重为"+weight+",颜色为"+color);
}
}
class UnspottedDog extends Dog{
UnspottedDog(String breed,int wegiht,String color){
super( breed,wegiht,color);
}
public void show(){
System.out.println("这是一只"+breed+"犬");
}
}
class SpottedDog extends Dog{
String spotColor;
SpottedDog(String breed,int wegiht,String color,String spotColor){
super(breed,wegiht,color);
this.spotColor=spotColor;
}
public void show(){
System.out.println("这是一只"+breed+"体重为"+weight+",颜色为"+color
);
System.out.println("这是一只"+breed+",颜色为"+color+",斑点颜色为"+spotColor);
}
}
public class Main {
public static void main(String[] args) {
String breed1,breed2;
int weight1,weight2;
String color1,color2;
String spotColor;
Scanner sc=new Scanner(System.in);
breed1=sc.next();
weight1=sc.nextInt();
color1=sc.next();
spotColor=sc.next();
breed2=sc.next();
weight2=sc.nextInt();
color2=sc.next();
SpottedDog d1=new SpottedDog(breed1,weight1,color1,spotColor);
d1.show();
UnspottedDog d2=new UnspottedDog(breed2,weight2,color2);
d2.show();
}
}
7174
跟上面一样的思路,建议熟悉了直接copy别人的,我就不传了
7175 后面的是接口和抽象类相关内容
|