继承之详细讲解
代碼結(jié)構(gòu)
package com.wuming.oop2.demo05; //person 人 父類 public class Person {//繼承:java只有單繼承,沒有多繼承,extends修飾public int money=10_0000_0000;private int money1=20;public int getMoney1() {return money1;}public void setMoney1(int money1) {this.money1 = money1;}public void say(){System.out.println("說了一句話");}// String看源碼//Object } package com.wuming.oop2.demo05; //學(xué)生 is 人 子類 public class Student extends Person{} package com.wuming.oop2.demo05; //Teacher is 人 子類 public class Teacher extends Person{} package com.wuming.oop2;import com.wuming.oop2.demo05.Person; import com.wuming.oop2.demo05.Student;public class Application {public static void main(String[] args) {Student student = new Student();student.say();System.out.println(student.money);// System.out.println(student.money1);//Error:(11, 35) java: money1可以在com.wuming.oop2.demo05.Person中訪問privateSystem.out.println(student.getMoney1());//Person給私有money1添加getter后可調(diào)用Person person = new Person();person.hashCode();//在java中所有的類都默認繼承Object類} }說了一句話
1000000000
20
總結(jié)
- 上一篇: Python abs函数 - Pytho
- 下一篇: C语言代码注释 - C语言零基础入门教程