- class Animal{
- //定义私有属性 age
- private static int age;
- //初始化 age
- Animal(){
- age = 1;
- }
- //定义私有方法getAge()
- static int getAge(){
- return age;
- }
- //定义公开方法setAge()
- public void setAge(int age){
- Animal.age = age;
- }
- //定义公开方法run()
- public void run(){
- //本方法的具体实现在继承类中完成
- }
- }
- class Dog extends Animal{
- public void run(){
- int age = Animal.getAge();
- System.out.println("Dog run..."+age);
- }
- }
- class Cat extends Animal{
- public void run(){
- int age = Animal.getAge();
- System.out.println("Cat run..."+age);
- }
- }
- public class Demo1 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Dog dog = new Dog();
- Cat cat = new Cat();
- dog.setAge(5);
- dog.run();
- cat.setAge(10);
- cat.run();
- }
- }
复制代码
|
|