1、继承
/**
* 父类
* @author Administrator
*
*/
public class FuLei {
public FuLei(){
System.out.println("先调用父类才调子类");
syjs();
}
private int i=1;
protected double d=12;
public float f=1.1f;
private void syjs(){
double j=i+d;
System.out.println(j);
System.out.println(i+d);
}
protected void mfjs(){
double j=f+i;
System.out.println(j);
}
public void gyjs(){
float j=f+i;
System.out.println("父类的方法"+j);
}
}
/**
* 子类
* @author Administrator
*
*/
public class ZiLei extends FuLei {
public ZiLei(){
super.gyjs();//调用父类的方法要用super关键字
System.out.println(this.d+" "+this.f);
gyjs();
this.mfjs();
}
public ZiLei(int c){ //构造重载
System.out.println("构造重载"+c);
}
public void gyjs(){ //方法重写
System.out.println("重写父类方法");
}
public void gyjs(int c){
System.out.println("方法重载"+c);
}
public static void main(String[] args) {
FuLei ziLei=new ZiLei();
ziLei.gyjs();
ZiLei ziLei2=new ZiLei(1);
ziLei2.gyjs(2);
ziLei2.mfjs();
}
}
2.接口
/**
* 接口
* @author Administrator
*
*/
public interface ITest {
// private void syff();//不能用private修饰
public void gyff();
// protected void mfff();//不能用protected修饰
// public static void jtff();//不能static修饰方法
// public final void zjff();//不能final修饰方法
static int i=10;
int j=10;
final int g=10;
static final int f=10;
}
/**
* 实现接口
* @author Administrator
*
*/
public class TestImpl implements ITest {
// 必须实现接口中的方法
public void gyff() {
// TODO Auto-generated method stub
System.out.println(this.i + " " + this.f + " " + this.g + " " + this.j);
}
public static void main(String[] args) {
ITest iTest = new TestImpl();
iTest.gyff();
}
}