zoukankan      html  css  js  c++  java
  • 抽象类

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

    }

    }

  • 相关阅读:
    2月8日
    2月7日
    2月6日
    2月5日
    事务
    synchronized关键字详解(二)
    synchronized关键字详解(一)
    java.sql.SQLException: Access denied for user 'somebody'@'localhost' (using password: YES)
    wex5 教程 之 图文讲解 wex5集成HTML5 视频播放器
    wex5 实战 加密与解密系列(1) DES算法引入与调用
  • 原文地址:https://www.cnblogs.com/batman425/p/4078859.html
Copyright © 2011-2022 走看看