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);
}
}