java基础知识——类的继承

1.在子类中通过super调用父类的方法,并将该调用嵌入复写该方法的过程中

package learning;

class Super {
     String nameString;
     int age;
public Super(String name, int age) { this.nameString = name; this.age = age; } public void talk(){ System.out.print("姓名:" + nameString + "\t\t" + "年龄:" + age); } } class Student extends Super{ String school; public Student(){ super("张三", 25 ); } //The method of Rewrite public void talk(){ super.talk(); System.out.println("\t\t" + "学校:" + school); } } /** * The method of super() * @author juefan */ public class Test1{ public static void main(String[] args){ Student student = new Student(); student.school = "华附"; student.talk(); } }

2.了解接口与接口实现的简单应用例子

package learning;

interface Usb{
    public void start();
    public void stop();
}
class Mp3 implements Usb{
    public void start(){
        System.out.println("Mp3 start... ");
    }
    public void stop(){
        System.out.println("Mp3 Stop...");
    }
}
class CD implements Usb{
    public void start(){
        System.out.println("CD start...");
    }
    public void stop(){
        System.out.println("CD stop...");
    }
}
class Computer{
    public void work(Usb usb){
        usb.start();
        usb.stop();
    }
}
/**
 * 以Usb类为接口类,Mp3跟CD都是Usb类的实现
 * Computer类会有Usb接口作为参数,调用Usb接口的实现输出
 * @author juefan
 *
 */
public class TestInterface {
    public static void main(String[] args){
        Computer computer = new Computer();
        computer.work(new Mp3());
        computer.work(new CD());
    }
}

可以看到Computer类中的work(Usb )方法是以Usb接口作为参数的

而Mp3跟CD都是Usb的接口的实现,这样就可以调用Mp3跟CD类中复写Usb接口中抽象方法的具体实现了,一个方法多种用处

posted on 2013-04-23 15:13  JueFan_C  阅读(156)  评论(0编辑  收藏  举报

导航