- abstract class Student
- {
- private String name;
- private int age;
- Student(String name,int age){
- this.setName(name);
- this.setAge(age);
- }
- protected void setName(String name){
- this.name=name;
- }
- protected String getName(){
- return this.name;
- }
- protected void setAge(int age){
- this.age=age;
- }
- protected int getAge(){
- return this.age;
- }
- abstract void study();
- protected void sleep(){
- System.out.println("躺着睡觉");
- }
- public String toString(){
- return this.getName()+","+this.getAge();
- }
- }
- class BaseStudent extends Student
- {
- BaseStudent(String name,int age){
- super(name,age);
- }
- void study(){
- System.out.println("base study");
- }
-
- }
- class AdvStudent extends Student
- {
- AdvStudent(String name,int age){
- super(name,age);
- }
- void study(){
- System.out.println("adv study");
- }
- public void sleep(){
- System.out.println("站着睡觉");
- }
- }
- class Do
- {
- protected void castClass(Student stu){
- stu.study();
- stu.sleep();
- }
- }
-
- public class Test
- {
- public static void main(String args[]){
- new Do().castClass(new BaseStudent("张三",22));
- }
- }
复制代码 |