java 两种方式的区别?

来源:互联网 发布:上海华腾软件公司地址 编辑:程序博客网 时间:2024/06/06 02:22
成员函数方式
package cn.com.ch09;class FatherTest{public void pet(String str){System.out.println(str);}}public class SonTest extends FatherTest {public void hitChild() {System.out.println("这是使用接口实现");}public static void main(String[] args) {SonTest son=new SonTest();son.pet("这是直接继承");son.hitChild();}}



另一种接口方式 

package cn.com.ch09;class FatherTest{public void pet(String str){System.out.println(str);}}interface Stepfather{public void hitChild();}public class SonTest extends FatherTest implements Stepfather{public void hitChild() {System.out.println("这是使用接口实现");}public static void main(String[] args) {SonTest son=new SonTest();son.pet("这是直接继承");son.hitChild();}}


原创粉丝点击