昨天我们大概的了解了一下Java父子类继承的关系,今天我通过我自己写的一个代码,进一步讲解和学习这方面的知识。
1.样例描述
今天以动物自我介绍为例,我们知道动物是一个大类,它包含着很多不同的动物,如小狗、小猫和老虎等。所以我们先创建一个大类 动物(Animal) ,然后在此基础上,创建各个子类,今天就以 小狗(Dog) 和 小猫(Cat) 为例。
类的属性和行为:
动物类(Animal): 名字(name)、颜色(color)、年龄(old);
小狗类(Dog): 智商(iq)、接飞盘(catch frisbee);
小猫类(Cat): 眼睛的颜色(eyeColor)、抓老鼠(catch mouse);
TestAnimal类: 由于输出动物的自我介绍。
每个动物都会以“My name is xxx, my color is xxx, my xx is xxx” 和 输出行为名称进行自我介绍
2.代码实现及讲解
父类 :动物类(Animal)
class Animal {
public String name;
public String color;
public int old;
public String catchAnimal() {
return "";
}
}
在动物类中,我们通过 catchAnimal() 方法来返回动物的行为信息
子类 : 小狗类(Dog)
class Dog extends Animal {
private final int iq;
public Dog( String name, String color, int iq, int old) {
this.iq = iq;
this.name = name;
this.color = color;
this.old = old;
}
public String toString() {
return "My name is " + name + ", My old is" + old + ", my color is " + color + ", my iq is " + iq;
}
public String catchAnimal() {
return "catch frisbee";
}
}
其中我们可以看到我们在子类中写了一个新方法 toString() 来返回小狗的属性信息,并且增加了一个它自己独有的属性智商(iq),我们通过改写父类Animal中的 catchAnimal() 方法实现子类对该方法的定制化需求,返回一个能满足子类要求的属性信息。
子类:小猫(Cat)
Cat extends Animal {
private final String eyeColor;
public Cat( String name, String color, String eyeColor, int old) {
this.eyeColor = eyeColor;
this.name = name;
this.color = color;
this.old = old;
}
public String toString() {
return "My name is " + name + ", My old is" + old + ", my color is " + color + ", my eyecolor is " + eyeColor;
}
public String catchAnimal() {
return "catch mouse";
}
}
在小猫类中,我们增加了它独有的属性 眼睛颜色(eyeColor),其他的与小狗类类似。
TestAnimal类:
class TestAnimal {
public static void introduce(Animal a) {
System.out.println(a.toString());
}
public static void action( Animal a) {
System.out.println(a.catchAnimal());
}
}
在 TestAnimal类 中, 我们通过编写两个静态的方法来输出动物自我介绍的信息, 其中 introduce(Animal a) 方法用来输出基本属性,action(Animal a) 方法用来输出行为特征。
3.代码合并
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int i = s.nextInt();
Animal a = null;
if (i == 1) {
a = new Cat(s.next(), s.next(), s.next(), s.nextInt());
} else if (i == 2) {
a = new Dog(s.next(), s.next(), s.nextInt(), s.nextInt());
}
TestAnimal.introduce(a);
TestAnimal.action(a);
}
}
class Animal {
public String name;
public String color;
public int old;
public String catchAnimal() {
return "";
}
}
class Dog extends Animal {
private final int iq;
public Dog( String name, String color, int iq, int old) {
this.iq = iq;
this.name = name;
this.color = color;
this.old = old;
}
public String toString() {
return "My name is " + name + ", My old is" + old + ", my color is " + color + ", my iq is " + iq;
}
public String catchAnimal() {
return "catch frisbee";
}
}
Cat extends Animal {
private final String eyeColor;
public Cat( String name, String color, String eyeColor, int old) {
this.eyeColor = eyeColor;
this.name = name;
this.color = color;
this.old = old;
}
public String toString() {
return "My name is " + name + ", My old is" + old + ", my color is " + color + ", my eyecolor is " + eyeColor;
}
public String catchAnimal() {
return "catch mouse";
}
}
class TestAnimal {
public static void introduce(Animal a) {
System.out.println(a.toString());
}
public static void action( Animal a) {
System.out.println(a.catchAnimal());
}
}
4.今日总结
(1)输入 next() 和 nextLine() 的区别,因为今天遇到了这个问题,next() 不会读入空格和换行,所以当你一行要输入几个字符串时可以考虑用 next() 输入。而 nextLine() 则不同,它会读入空格,直到遇到换行才结束,所以一般用于整行的输入。
(2)对于父子类之间方法的调用和改写,还有些生疏,如何通过 super() 关键字来调用父类的构造方法?