Java基础29-子父类中的成员变量

 1 /*
 2 成员:
 3 1.成员变量
 4 2.函数
 5 3.构造函数
 6 
 7 变量:
 8 this 代表当前对象的引用 this.变量    首先在本类中找所需要的变量,如果没有找到再父类中找。
 9 super 用于访问当前对象的父类成员,super.变量    直接父类中找所需变量
10 */
11 
12 class Person{
13     String name="张三";
14     
15 }
16 
17 class Student extends Person{
18     String name="李四";
19     void show(){
20         System.out.println(name);//默认this.name 输出李四
21         System.out.println(super.name);//输出张三,如果父类中没有该变量则提示失败
22     }
23     
24 }
25 
26 public class Test{
27     public static void main(String[] args){
28         Student stu=new Student();
29         stu.show();
30     }
31 }
原文地址:https://www.cnblogs.com/shenhainixin/p/10111036.html