java接口的继承和实现_Java继承和接口实现

package com.what21.base09;

/**

* 继承

*/

public class ExtendsTest {

public static void main(String[] args) {

// 参数为空的构造方法,如果没有其他构造方法,系统自动提供该构造方法。

// 如果有其他构造方法,系统不会自动提供,需要手动编写

A a = new A(1111111);

a.print();

// 继承A类

// java不支持多继承

SubA subA = new SubA(2222222);

// 继承了A类的属性

System.out.println(subA.i);

// 继承了A类的方法

subA.print();

subA.plus();

}

}

class A{

int i;

A(){

// 空构造方法

}

A(int i){

// 带参数的构造方法

this.i = i;

}

public void print(){

System.out.println(i);

}

}

class SubA extends A{

SubA(int i){

// 调用父类带参数的构造方法

super(i);

}

public void plus(){

System.out.println(i+i);

}

}

package com.what21.base09;

/**

* 接口实现

*/

public class ImplTest {

public static void main(String[] args) {

// 实现IA接口

SubImplA subA = new SubImplA();

// 实现IA接口中的print方法

subA.print();

// 实现IA接口

SubImplB subB = new SubImplB();

// 实现IA接口中的print方法

subB.print();

}

}

interface IA{

public static final String A = "aaaaaa";

/**

* 接口方法定义

*/

public void print();

}

class SubImplA implements IA{

@Override

public void print() {

// 实现IA接口中的print方法

System.out.println(A);

}

}

class SubImplB implements IA{

@Override

public void print() {

// 实现IA接口中的print方法

System.out.println(A + A);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值