首页 > 其他 > 详细

抽象类

时间:2014-11-06 16:26:35      阅读:74      评论:0      收藏:0      [点我收藏+]

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

}

}

抽象类

原文:http://www.cnblogs.com/batman425/p/4078859.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!