学习继承、接口

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

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值