一、繼承
java
擴展父類的功能
this
二、使用extends關鍵字完成繼承spa
class 子類 extends 父類{}
code
package heh; public class jic { public static void main(String[] args) { // TODO Auto-generated method stub Student stu=new Student(); stu.setName("zhangsan"); stu.setAge(19); stu.setScore(40); stu.tell(); } } class Person{ private String name; private int age; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } } class Student extends Person{ // private String name; // private int age; private int score; /** * @return the name */ // public String getName() { // return name; // } /** * @param name the name to set */ // public void setName(String name) { // this.name = name; // } /** * @return the age */ // public int getAge() { // return age; // } /** * @param age the age to set */ // public void setAge(int age) { // this.age = age; // } /** * @return the score */ public int getScore() { return score; } /** * @param score the score to set */ public void setScore(int score) { this.score = score; } public void tell(){ System.out.println("name:"+getName()+" age:"+getAge()+" score:"+getScore()); } }
繼承的限制對象
一、在java中只容許單繼承繼承
二、子類不能直接訪問父類的私有成員(經過get,set方法設置和獲得)get
package heh; class People{ int age; } class Worker extends People{ } class Worker2 extends People{ } class PetWorker extends Worker{ public void tell(){ System.out.println(age); } } /** * 如下這種繼承是錯誤的,容許多層繼承,不容許下面的,就像一個兒子只有一個親生父親 class PetWorker extends Worker,People{ } */ public class ExtDemo02 { public static void main(String[] args) { // TODO Auto-generated method stub } }
package heh; class People{ private int age; /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } } class Worker extends People{ public void tell(){ System.out.println("Age:"+getAge()); } } public class ExtDemo02 { public static void main(String[] args) { // TODO Auto-generated method stub Worker pe=new Worker(); pe.setAge(21); pe.tell(); } }
子類對象的實例化class
一、在子類對象實例化以前,必須先調用父類中的構造方法,以後調用子類構造方法擴展
package heh; class Father{ private String name; public Father(){ System.out.println("父類中的構造方法"); } } class Son extends Father{ public Son(){ System.out.println("子類中的構造方法"); } } public class dd { public static void main(String[] args) { // TODO Auto-generated method stub Son s=new Son(); } }
Java方法重寫與super關鍵字權限
一、在繼承中,也存在着重寫的概念,其實就是子類定義了和父類同名的方法
二、定義:
方法名稱相同,返回值類型相同,參數也同。
三、重寫限制:
被子類重寫的方法不能擁有比父類方法更加嚴格的訪問權限
四、訪問權限
private(只能在當前類中進行訪問)<default(默認的訪問權限,可以在整個包中)<public(整個項目中)
super關鍵字
強行執行父類方法的執行(構造方法(方法名和類名同樣)中,不用添加,他會自動添加super()關鍵字)
package heh; class A{ public void tell(){ System.out.println("我是父類方法"); } private void say(){ } void print(){ //default (默認的訪問權限) } } class B extends A{ public void tell(){ super.tell(); System.out.println("我重寫了tell方法"); } /**如下出錯了, 被子類重寫的方法不能擁有比父類方法更加嚴格的訪問權限 private void print(){ } */ } public class extDemo04 { public static void main(String[] args) { // TODO Auto-generated method stub B b=new B(); b.tell(); } }
二、super不必定在重寫中使用,也能夠表示那些方法時從父類中繼承而來的
ava重寫與重載的區別