extends在java中是什么意思_java中extends的意思是什么

java中extends的意思是什么

发布时间:2020-07-29 10:35:12

来源:亿速云

阅读:79

作者:Leah

今天就跟大家聊聊有关java中extends的意思是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。extends在java中的作用是继承的意思,在Java中,通过关键字extends继承一个已有的类,被继承的类称为父类【超类,基类】,新的类称为子类【派生类】,并且在Java中不允许多继承。

继承是理解面向对象程序设计的关键。在Java中,通过关键字extends继承一个已有的类,被继承的类称为父类(超类,基类),新的类称为子类(派生类)。在Java中不允许多继承。class Animal{

void eat(){

System.out.println("Animal eat");

}

void sleep(){

System.out.println("Animal sleep");

}

void breathe(){

System.out.println("Animal breathe");

}

}

class Fish extends Animal{

}

public class TestNew {

public static void main(String[] args) {

// TODO Auto-generated method stub

Animal an = new Animal();

Fish fn = new Fish();

an.breathe();

fn.breathe();

}

}

在eclipse执行得:

Animal breathe!

Animal breathe!

.java文件中的每个类都会在文件夹bin下生成一个对应的.class文件。执行结果说明派生类继承了父类的所有方法。

覆盖class Animal{

void eat(){

System.out.println("Animal eat");

}

void sleep(){

System.out.println("Animal sleep");

}

void breathe(){

System.out.println("Animal breathe");

}

}

class Fish extends Animal{

void breathe(){

System.out.println("Fish breathe");

}

}

public class TestNew {

public static void main(String[] args) {

// TODO Auto-generated method stub

Animal an = new Animal();

Fish fn = new Fish();

an.breathe();

fn.breathe();

}

}

执行结果:

Animal breathe

Fish breathe

在子类中定义一个与父类同名,返回类型,参数类型均相同的一个方法,称为方法的覆盖。方法的覆盖发生在子类与父类之间。另外,可用super提供对父类的访问。

看完上述内容,你们对java中extends的意思是什么有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值