其实可以验证是父类的,你在父类里面给name赋值。再实例化Cat,调用function()就知道了。
- package com.study.two;
- class Animal{
- String name="dongwu";
- int age;
- public void run(){
- System.out.println("animal run");
- }
- public void eat(){
- System.out.println("animal eat");
- }
- public void roar(String name,int age){
- System.out.println("animal talk");
- }
- }
- class Dog extends Animal{
- public void eat(){
- System.out.println("吃肉");
- }
- public void roar(String name,int age){
- System.out.println("小汪名叫"+name+",今年"+age+"汪汪~~~");
- }
- }
- class Cat extends Animal{
- public void eat(){
- System.out.println("吃鱼");
- }
- public void function(){
- System.out.println(super.name); //这里的super指的是那里???
- }
- public void function2(){
- System.out.println(this.name);
- }
- public void roar(String name,int age){
- this.name = name;
- this.age = age;
- System.out.println("小喵名叫"+name+",今年"+age+"岁");
- }
- }
- class ZuoYe{
- public static void main(String[] args){
-
- Cat c=new Cat();
- c.function();
-
- /* Animal d = new Dog();
- method(d);
-
- Animal c = new Cat();
- method(c);*/
-
- /* c.roar("阿喵",3);
- System.out.println("__________________");
- Cat cc = (Cat)c;
- cc.function();
- System.out.println("__________________");
- cc.function2();
- System.out.println("__________________");
- Animal a = new Animal();
- System.out.println(a.name);*/
- }
- public static void method(Animal a){
- a.run();
- a.eat();
- }
- }
复制代码 |