麻烦各位java大神网友,万分感谢帮忙看看解答一下下面编程题?

2024-12-28 21:39:11
推荐回答(1个)
回答1:

hnjm.xxgl包:
//Animal.java文件

package hnjm.xxgl;

public class Animal {
protected String animalType = null;

public void printAnimalType() {
System.out.println("animalType : " + animalType);
}

void shout() {
System.out.println("shout");
}

protected void active() {
System.out.println("active");
}
}

//Cat.java文件
package hnjm.xxgl;

public class Cat extends Animal {
private void zhuoShu() {
System.out.println("zhuoShu");
}
}

//Test.java文件
package hnjm.xxgl;

public class Test {
public static void main(String[] args) {
Cat cat = new Cat();
cat.printAnimalType();
cat.shout();
cat.active();
//cat.zhuoShu(); //不允许访问
}
}

hnjm.xxgl.test包:

//Bird.java文件
package hnjm.xxgl.test;

import hnjm.xxgl.Animal;

public class Bird extends Animal {
public void sing() {
System.out.println("鸟儿在歌唱");
}
}

//Test.java文件
package hnjm.xxgl.test;

public class Test {
public static void main(String[] args) {
Bird bird = new Bird();
bird.printAnimalType();
//bird.shout(); //不允许访问
//bird.active(); //不允许访问
bird.sing();
}
}