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

验证访问控制权限

(1)创建两个包:hnjm.xxgl、hnjm.xxgl.test包

(2)在hnjm.xxgl包中创建一个类Animal,定义一个属性animalType,并封装。该类中定义3个无返回值的方法:第一个方法printAnimalType(),用于输出animalType属性值,使用public修饰;第二个方法shout(),用于描述动物的叫声,不使用任何访问控制修饰符;第三个方法active(),用于描述动物行走,使用protected修饰;

(3)在hnjm.xxgl包中再创建一个类Cat,该类继承Animal类,并定义一个方法zhuoShu(),设置为私有,在主方法中创建Cat对象并调用调用父类和自己的方法,判断是否能全部继承;

(4)在hnjm.xxgl.test包中创建一个类Bird,继承Animal类,并定义一个方法sing(),打印输出“鸟儿在歌唱”;

(5)在hnjm.xxgl.test包定义测试类的名字使用姓名拼音以及作业序号,在主方法中创Bird对象,并调用从Animal继承的方法以及自己的方法,观察父类的方法是否能全部继承。

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();
}
}
温馨提示:答案为网友推荐,仅供参考
相似回答