知识点
- 继承
就是子类继承父类的属性和行为,使得子类对象具有与父类相同的属性、相同的行为。子类可以直接 访问父类中的非私有的属性和行为。
好处:1. 提高代码的复用性。 2. 类与类之间产生了关系,是多态的前提。
格式:
通过 extends 关键字,可以声明一个子类继承另外一个父类
class 父类 {
…
}
class 子类 extends 父类 {
…
}
- 方法重写
子类中出现与父类一模一样的方法时(返回值类型,方法名和参数列表都相同),会出现覆盖效 果,也称为重写或者复写。声明不变,重新实现
课堂实例:
package com.hg.day09.demo01;
public class Teacher extends Employee {
public void methodChildren() {
System.out.println("我是子类--老师的方法");
System.out.println("我要讲课");
}
}
package com.hg.day09.demo01;
public class Employee {
String name;
int age;
public void method() {
System.out.println("我是父亲--雇员的方法");
}
}
package com.hg.day09.demo01;
public class Assistant extends Employee {
public void methodChildren() {
System.out.println("我是子类--助教的方法");
System.out.println("我要改作业");
}
}
package com.hg.day09.demo01;
public class Demo01Extends {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.method();
teacher.methodChildren();
Assistant assistant = new Assistant();
assistant.method();
assistant.methodChildren();
}
}
package com.hg.day09.demo02;
public class Fu {
int numFu = 10;
int num = 100;
public void methodFu() {
System.out.println("我是父亲的方法"+num);
}
}
package com.hg.day09.demo02;
public class Zi extends Fu {
int numZi = 20;
// int num = 200;
public void methodZi() {
// int num = 2000;
System.out.println("我是子类的方法:"+num);
}
}
package com.hg.day09.demo02;
public class Demo02Extend {
public static void main(String[] args) {
Fu fu = new Fu();
System.out.println(fu.numFu);
System.out.println("========");
Zi zi = new Zi();
System.out.println(zi.numFu);
System.out.println(zi.numZi);
System.out.println("========");
System.out.println(zi.num);
System.out.println("========");
zi.methodZi();
}
}
package com.hg.day09.demo03;
public class Fu {
public void methodFu() {
System.out.println("我是父亲方法--methodFu");
}
public void method() {
System.out.println("我是父亲方法--method");
}
}
package com.hg.day09.demo03;
public class Zi extends Fu{
public void methodZi() {
System.out.println("我是子类方法--methodZi");
}
@Override
public void method() {
System.out.println("我是子类方法--method");
}
}
package com.hg.day09.demo03;
public class child extends Fu {
}
package com.hg.day09.demo03;
public class Demo03Extends {
public static void main(String[] args) {
Zi zi =new Zi();
zi.methodFu();
zi.methodZi();
zi.method();
Fu fu = new Fu();
}
}
package com.hg.day09.demo04;
public class Phone {
public void call() {
System.out.println("打电话");
}
public void send() {
System.out.println("发短信");
}
public void show() {
System.out.println("老手机显示号码");
}
}
package com.hg.day09.demo04;
public class NewPhone extends Phone{
public void show() {
super.show();
System.out.println("显示头像");
System.out.println("显示归属地");
}
}
package com.hg.day09.demo04;
public class Demo04Extends {
public static void main(String[] args) {
Phone phone = new Phone();
phone.call();
phone.send();
System.out.println("====");
phone.show();
System.out.println("===========");
NewPhone newphone = new NewPhone();
newphone.call();
newphone.send();
System.out.println("====");
newphone.send();
}
}
package com.hg.day09.demo05;
public class Fu {
public Fu() {
System.out.println("父亲无参构造方法");
}
public Fu(int num) {
System.out.println("父亲有一个参数的 有参构造方法");
}
public Fu(int num,String name) {
System.out.println("父亲有两个参数的 有参构造方法");
}
}
package com.hg.day09.demo05;
public class Zi extends Fu {
public Zi() {
super(20,"成龙大哥");
System.out.println("子类无参数构造方法");
}
}
package com.hg.day09.demo05;
public class Demo05Extends {
public static void main(String[] args) {
Zi zi = new Zi();
}
}
package com.hg.day09.demo06;
public class Fu {
int num = 100;
public void method() {
System.out.println("父类方法");
}
}
package com.hg.day09.demo06;
public class Zi extends Fu {
int num = 20;
public Zi() {
super();
}
public void method() {
System.out.println("子类方法");
}
public void methodZi() {
System.out.println(num);
System.out.println(super.num);
super.method();
}
}
package com.hg.day09.demo06;
public class Demo06Extends {
public static void main(String[] args) {
Zi zi = new Zi();
zi.methodZi();
}
}