码迷,mamicode.com
首页 > 其他好文 > 详细

抽象类

时间:2014-11-06 16:29:46      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:io   ar   使用   on   bs   ad   new   as   res   

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

}

}

抽象类

标签:io   ar   使用   on   bs   ad   new   as   res   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!