java——抽象类的使用

2019年2月16日10:37:46 发表评论 946 views
/**
 * Created by xabcd on 2019/2/16.
 */
abstract class java_chouxiang1
{String name;
    int age;
    String occupation;
    //声明一种抽象方法talk()
    public java_chouxiang1(String name,int age,String occuupation)
    {
        this.name = name;
        this.age = age;
        this.occupation= occuupation;
    }
    public abstract String talk();
}
//Student4类继承自java_chouxiang1类
class Student4 extends java_chouxiang1
{
   public Student4(String name,int age,String occupation) {
//       //在这里必须明确调用抽象类中的构造方法(抽象类中声明构造方法后,需要在子类中明确调用)
       super(name,age, occupation);
   }
       //覆写talk()方法
       public String talk()
       {
           return "学生——》姓名:" + this.name + ",年龄:" + this.age + "职业:" + this.occupation;
       }

   }




/**
 * Created by xabcd on 2019/2/16.
 */
public class test_chouxiang
{
    public static void main(String[] args)
    {
        Student4 s = new Student4("张三",20,"学生");
        System.out.println(s.talk());

    }
}





学生——》姓名:张三,年龄:20职业:学生

			

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: