一、继承
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重写与重载的区别