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

一个类只可以继承一个类,
一个接口却可以继承多个接口。
父类的方法,子类继承后如果重写。则创建子类对象后使用该方法调用的是子类中的该方法。 否则,即没有修改父类里的方法,调用的就仍是父类方法。
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 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值