标签:extend ext main 一个 优先 style bsp ring str
当一个类继承于另一个类,当调用方法时,优先在子类中查找,再才是从父类。
class A { int x = 6; private int y = 2; public A(int a) { x = a; } int getz() { int z; z = x / y; return z; } void show() { System.out.println("x=" + x); System.out.println("y=" + y); System.out.println("z=" + getz()); } } class B extends A { int x = 3, y = 5, z; public B(int a) { super(a); } int getz() { z = x + y; return z; } } public class Temp { public static void main(String[] args) { A num1 = new A(10); B num2 = new B(9); num1.show(); num2.show(); } }
标签:extend ext main 一个 优先 style bsp ring str
原文地址:https://www.cnblogs.com/xiaoliu66007/p/11214093.html