• 抽象类


    package AbstractTest;
    /*
    * 抽象类和抽象方法都必须有关键字abstract来修饰
    * 抽象类不能实例化,也不能使用关键字new来产生对象
    * 抽象方法只需声明,不需实现
    * 含有抽象方法的类必须被声明为抽象类,抽象类必须复写所有抽象方法后
    * 才能被实例化,或者这个子类就是抽象类
    * */
    abstract class Persion{
    String name;
    int age;
    String occupation;
    /*声明一个构造方法,在子类中必须被调用*/
    public Persion(String name,int age,String occupation){
    this.name = name;
    this.age = age;
    this.occupation = occupation;
    }
    /*声明一个抽象的方法*/
    public abstract String talk();
    /*一般方法*/
    public int sum(){
    int a=3;
    int b=4;
    return a+b;
    }
    }

    class Student extends Persion{
    String address;
    public Student(String name,int age,String occupation){
    /*在这里必须明确调用抽象类中的构造方法*/
    super(name,age,occupation);
    /*this.name = name;
    this.age = age;
    this.occupation = occupation; */
    address = "上海通联金融服务有限公司";
    }
    /*复写talk()方法*/
    public String talk(){
    return "学生--->姓名: "+this.name+" 年龄: "+this.age+" 职业:"+
    this.occupation+" 地址:"+address+" 人数:"+super.sum();
    }

    public String tolk(){
    return "学生--->姓名: "+this.name+" 年龄: "+this.age+" 职业:"+
    this.occupation+" 地址:"+address+" 人数:"+super.sum();
    }
    }

    /**
    *class Worker extends Persion{
    * public Worker(String name,int age,String occupation){
    * this.name = name;
    * this.age = age;
    * this.occupation = occupation;
    * }
    * public String talk(){
    * return "工人--->姓名: "+this.name+" 年龄: "+this.age+" 职业:"+
    * this.occupation;
    * }
    *}
    */
    public class AbstractTest {

    public AbstractTest() {
    // TODO Auto-generated constructor stub
    }

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Student st = new Student("张三",22,"学生");
    // Worker wk = new Worker("李四",30,"工人");
    System.out.println(st.tolk());
    // System.out.println(wk.talk());

    }

    }

  • 相关阅读:
    C++ template —— 模板基础(一)
    《C++标准程序库》笔记之四
    《C++标准程序库》笔记之三
    《C++标准程序库》笔记之二
    C++标准程序库笔记之一
    JAVA中JPA的主键自增长注解设置
    SVN中服务器地址变更
    JAVA中正则表达式常用的四个方法
    反编译class文件并重新编译的方法
    JAVA中文件与Byte数组相互转换的方法
  • 原文地址:https://www.cnblogs.com/batman425/p/4078859.html
Copyright © 2020-2023  润新知