在Java中,通过关键字返闭extends继承一个已
有的类,被继承的类称为父类(超类,基类),新的类隐型称为
子类(派生类)。在Java
中不允许多继承。漏携裂\x0d\x0a举例:\x0d\x0a\x0d\x0aclass Animal{ \x0d\x0a void eat(){ \x0d\x0a System.out.println("Animal eat") \x0d\x0a } \x0d\x0a void sleep(){ \x0d\x0a System.out.println("Animal sleep") \x0d\x0a } \x0d\x0a void breathe(){ \x0d\x0a System.out.println("Animal breathe") \x0d\x0a } \x0d\x0a} \x0d\x0a \x0d\x0aclass Fish extends Animal{ \x0d\x0a void breathe(){ \x0d\x0a System.out.println("Fish breathe") \x0d\x0a } \x0d\x0a} \x0d\x0a \x0d\x0apublic class TestNew { \x0d\x0a public static void main(String[] args) { \x0d\x0a Animal an = new Animal() \x0d\x0aFish fn = new Fish() \x0d\x0aan.breathe() \x0d\x0afn.breathe() \x0d\x0a\x0d\x0a } \x0d\x0a} \x0d\x0a执行结果:\x0d\x0aAnimal breathe\x0d\x0aFish breathe在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{
void
breathe(){
System.out.println("Fish
breathe")
}
}
public
class
TestNew
{
public
static
void
main(String[]
args)
{
Animal
an
=
new
Animal()
Fish
fn
=
new
Fish()
an.breathe()
fn.breathe()
}
}
执行结果:皮蚂
Animal
breathe
Fish
breathe
评论列表(0条)