您的位置:首页 > 编程语言 > Java开发

Java 接口,继承 用法意外体会。

2011-12-26 18:24 211 查看
一个类只可以继承一个类,

一个接口却可以继承多个接口。

父类的方法,子类继承后如果重写。则创建子类对象后使用该方法调用的是子类中的该方法。 否则,即没有修改父类里的方法,调用的就仍是父类方法。

package 复习;

class Base{

void f(){System.out.println("调用的是父类里的方法!");}

}

public class Test extends Base {

public void f(){

//super.f();

System.out.println("调用的是子类里的方法!");

}

public static void main(String[] args) {

Base b1=new Test();

Test t1=new Test();

b1.f();

t1.f();

}

}

调用的是子类里的方法!

调用的是子类里的方法!

package 复习;

class Base{

void f(){System.out.println("调用的是父类里的方法!");}

}

public class Test extends Base {

public void f(){

//super.f();

System.out.println("调用的是子类里的方法!");

}

public static void main(String[] args) {

Base b1=new Base();

Test t1=new Test();

b1.f();

t1.f();

}

}

调用的是父类里的方法!

调用的是子类里的方法!

一个接口可以继承多个接口,最终某类再执行该接口(集成了多个接口的接口)时,必须实例化所有抽象函数。 当然,这个类同时也可以继承一个父类,倘若该父类中的 方法 shortage()在接口AsianMan中已有,这个子类依然要实例化shortage()方法,且该子类对象在调用时使用的是接口中的shortage()!!!

package interfaces;

interface Animal{

void eat();

}

interface Human{

void drink();

}

interface Man{

void run();

}

interface AsianMan extends Animal,Human,Man{

void shortage();

}

class HeNanMan{

void shortage(){

System.out.println("But he was slightly inferior, especially in matters of taste and style .");

}

}

class ME extends HeNanMan implements AsianMan{

public void eat(){System.out.print("This type can eat, ");}

public void drink(){System.out.print("can drink,");}

public void run(){System.out.print(" can run.");}

public void shortage(){System.out.println("But he has few ambition !!!");}

}

public class TryInterface {

public static void main(String[] args) {

ME fc=new ME();

fc.eat();

fc.drink();

fc.run();

fc.shortage();

//fc.shortage();

}

}

output: This type can eat, can drink, can run.But he has few ambition !!!

假使没有冲突,那都可以调用。

package interfaces;

interface Animal{

void eat();

}

interface Human{

void drink();

}

interface Man{

void run();

}

interface AsianMan extends Animal,Human,Man{

void shortage();

}

class HeNanMan{

void shortage2(){

System.out.println(" and he was slightly inferior, especially in matters of taste and style .");

}

}

class ME extends HeNanMan implements AsianMan{

public void eat(){System.out.print("This type can eat, ");}

public void drink(){System.out.print("can drink,");}

public void run(){System.out.print(" can run.");}

public void shortage(){System.out.print("But he has few ambition !!!");}

}

public class TryInterface {

public static void main(String[] args) {

ME fc=new ME();

fc.eat();

fc.drink();

fc.run();

fc.shortage();

fc.shortage2();

}

}

output: This type can eat, can drink, can run.But he has few ambition !!! and he was slightly inferior, especially in matters of taste and style .

接口可以选择性继承,只执行一部分抽象方法

package interfaces;

interface Animal{

void eat();

}

interface Human{

void drink();

}

interface Man{

void run();

}

interface AsianMan extends Animal,Human{

void shortage();

}

class HeNanMan{

void run(){System.out.print("can run more faster than other province. ");}

void shortage2(){

System.out.println("and he was slightly inferior, especially in matters of taste and style .");

}

}

class ME extends HeNanMan implements AsianMan{

public void eat(){System.out.print("This type can eat, ");}

public void drink(){System.out.print("can drink,");}

//public void run(){System.out.print(" can run.");}

public void shortage(){System.out.print("But he has few ambition !!!");}

}

public class TryInterface {

public static void main(String[] args) {

ME fc=new ME();

fc.eat();

fc.drink();

fc.run();

fc.shortage();

fc.shortage2();

}

}

output: This type can eat, can drink,can run more faster than other province. But he has few ambition !!!and he was slightly inferior, especially in matters of taste and style .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: