public void show() {
System.out.println(num); System.out.println(num2); } }
class Son extends Father {
public void function() {
//num可以在Father中访问private //System.out.println(num); //子类不能继承父类的私有成员变量 System.out.println(num2); } }
class ExtendsDemo3 {
public static void main(String[] args) {
// 创建对象 Son s = new Son(); //s.method(); //子类不能继承父类的私有成员方法 s.show(); s.function(); } }
1.3、继承中成员变量和成员方法的调用
继承中成员变量的调用规则
/*
继承中成员变量的关系: 在子类方法中访问一个变量的查找顺序: a:在子类方法的局部范围找,有就使用 b:在子类的成员范围找,有就使用 c:在父类的成员范围找,有就使用 d:如果还找不到,就报错。 */ class Father {
public int num = 10;
public void method() {
int num = 50; } }
class Son extends Father {
public int num2 = 20; public int num = 30;
public void show() {
int num = 40; System.out.println(num); System.out.println(num2); // 找不到符号,报错 //System.out.println(num3); } }
class ExtendsDemo4 {
public static void main(String[] args) {
//创建对象 Son s = new Son(); s.show(); } }
继承中成员方法的调用规则:
/* 继承中成员方法的关系: 通过子类对象调用方法: a:先找子类中,看有没有这个方法,有就使用 b:再看父类中,有没有这个方法,有就使用 c:如果没有就报错。 */ class Father {
public void show() {
System.out.println("show Father"); } }
class Son extends Father {
public void method() {
System.out.println("method Son"); }
public void show() {
System.out.println("show Son"); } }
class ExtendsDemo8 {
public static void main(String[] args) {
//创建对象 Son s = new Son(); s.show(); s.method(); //s.fucntion(); //找不到符号 } }