java继承

继承
  • 1、提高了代码的复用性,简化了代码
  • 2、让类与类之间产生继承关系,才有了后面的多态特性的存在
//父类,基类,超类
class Person5{
	String name;
	int age;
}

//子类
class Student8 extends Person5{ //继承用 extends 父类名
	void study(){
		System.out.println("学习");
	}
}

//子类
class Teacher extends Person5{
	void teach(){
		System.out.println("教书");
	}
}

public class test_extends {
	public static void main(String[] args){
		Student8 S = new Student8();
		S.study();
		System.out.println(S.age); 
	}
}
  • 子类拥有父类的成员,子类不能拥有父类中被private修饰后的成员
  • java多层继承,在多层继承中最下层的子类拥有整个继承体系的成员,最上层的父类里面定义所有子类的共性描述
  • java中不支持多继承,只支持单继承。因为多继承存在安全隐患,当多个父类定义了相同的成员,子类对象不确定运行哪一个
// 父类 动物
class Animal{
	String buru = "哺乳";
}
// 第一级子类 猫科
class MaoKe extends Animal{
	String color;
	void eat(){
		System.out.println("吃");
	}
}
// 第二级子类猫
class Cat1 extends MaoKe{
	void CatchMouse(){
		System.out.println("抓老鼠");
	}
}
//第二级子类猎豹
class Leopaed extends MaoKe{
	void run(){
		System.out.println("奔跑");
	}
}

public class test_extends1 {
	public static void main(String[] args){
		Cat1 c = new Cat1();
		System.out.println(c.buru); //第一级父类的成员变量
		System.out.println(c.color);  //第二级父类的成员变量
		c.eat();
	}
}

成员:成员变量、函数、构造函数

  • this 代表当前对象的引用 this. 变量 首先在本类中找所需要的这个变量,如果没有找到再从父类中找
  • super 用于访问当前对象的父类成员, super . 变量 直接在父类中找所需变量
//父类
class Person7{
	String name = "何艺";
}
//子类
class Student7 extends Person7{
	String name = "Hey";
	void show(){
		System.out.println(super.name);  //子类调用父类需要super
		System.out.println(name); 
	}
}
public class test_extends2 {
	public static void main(String[] args){
		Student7 stu = new Student7();
		stu.show();
	}
}

class Weapen{  //武器
	public String power = "我具有攻击力哦";
	public int speed = 360;

	Weapen(){
		System.out.println("我是父类的构造方法");
	}
}

class Tank extends Weapen{  //坦克
	private String dir = "左";
	public void atack(){
		System.out.println("我是坦克,向"+dir+"方向移动,速度"+super.speed+"攻击力"+super.power);
	}
}
public class test_extends6 {
	public static void main(String[] args){
		Tank t = new Tank();
		t.atack();
	}
}
  • 当子类中出现和父类中一样的函数时,当子类对象调用该函数,运行的是子类中的函数,如同父类中的函数被覆盖了,这种情况就是函数的另一种特性:重写(覆盖)
    注意:
  • 子类覆盖父类时,必须保证子类权限大于等于父类,才可以覆盖,否则编译出错
  • 访问权限修饰符:public > default > private
  • 成员前面没加任何访问权限,修饰符默认权限为default
  • 静态的函数只能覆盖静态的

区分:

  • 重载:只看同名的方法参数列表
  • 重写:子父类方法要一模一样
class Animal1{
	String type;
	void run(){
		System.out.println("跑步中");
	}
}
class Dog1 extends Animal1{
	void run(){
		type = "小白";
		System.out.println(type+"正跑过来");
	}
}
public class test_extends3 {
	public static void main(String[] args){
		Dog1 dog = new Dog1();
		dog.run();
	}
}

class Animal2{
	void show(){
		System.out.println("我是父类");
	}
	void sleep(){
		System.out.println("动物在睡觉");
	}
}

class Cat2 extends Animal2{
	void show(String name){
		System.out.println(name);
	}
	void sleep(){
		System.out.println("猫在睡觉");
	}
}
public class test_extends5 {
	public static void main(String[] args){
		Cat2 cat = new Cat2();
		cat.show("小猫咪");
		cat.sleep();
	}
}

有了子父类之后构造函数 的特点:

  • 在对子类对象进行初始化的时候,父类的构造方法也会运行,那是因为子类的构造函数默认第一行有一条隐式语句 super();
  • 子类每个构造方法第一行都有一个隐式的 super();
class fu{
	String name;
	fu(){
		System.out.println("我是父类构造方法");
	}
	fu(String name){
		this.name = name;
		System.out.println("我是父类有参构造方法");
		}
}
class zi extends fu{
	zi(){
		super();
		System.out.println("我是子类构造方法");
	}
	zi(String name){
		super();
		this.name = name;
		System.out.println("我是子类有参构造方法");
		System.out.println(this.name);
	}
}

public class test_extends11 {
	public static void main(String[] args){
		zi mz = new zi("karry");
	}
}

//父类
class Students1{
	String name;
	int age;
	String degree;
	Students1(){
		System.out.println("父类构造函数");
	}
	void show(){
		System.out.println(name+age+degree);
	}
}
//子类
class Undergraduate extends Students1{
	String specialty;
	void show(){
		System.out.println(specialty);
	}
}
class Graduate extends Students1{
	String direction;
	void show(){
		System.out.println(direction);
	}
}
public class extends_student {
	public static void main(String[] args){
		Undergraduate udg = new Undergraduate();
		udg.degree="本科";
		System.out.println(udg.degree);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值