黑马程序员技术交流社区
标题:
【记录】代码练习-抽象类
[打印本页]
作者:
Kevin.Kang
时间:
2015-6-29 23:04
标题:
【记录】代码练习-抽象类
本帖最后由 Kevin.Kang 于 2015-6-30 10:11 编辑
abstract class Animal
{
private String name;
private int age;
public Animal(){}
public Animal(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public abstract void eat();
}
class Dog extends Animal
{
public Dog(){}
public Dog(String name, int age)
{
super(name,age);
}
public void eat()
{
System.out.println("狗吃肉");
}
}
class Cat extends Animal
{
public Cat(){}
public Cat(String name, int age)
{
super(name,age);
}
public void eat()
{
System.out.println("猫吃鱼");
}
}
class AbstractTest
{
public static void main (String [] args)
{
//狗类
//方式1
Dog d = new Dog();
d.setName("旺财");
d.setAge(2);
System.out.println(d.getName()+":"+d.getAge());
d.eat();
System.out.println("==============");
//方式2
Dog d2 = new Dog("大黄",3);
System.out.println(d2.getName()+":"+d2.getAge());
d2.eat();
System.out.println("==============");
//方式3
Animal d3 = new Dog();
d3.setName("汪汪");
d3.setAge(1);
System.out.println(d3.getName()+":"+d3.getAge());
d3.eat();
System.out.println("==============");
//方式4
Animal d4 = new Dog("旭狗",4);
System.out.println(d3.getName()+":"+d3.getAge());
d3.eat();
System.out.println("--------------");
//猫类
//方式1
Cat c = new Cat();
c.setName("喵喵");
c.setAge(1);
System.out.println(c.getName()+":"+c.getAge());
c.eat();
System.out.println("==============");
//方式2
Cat c2 = new Cat("小喵",2);
System.out.println(c2.getName()+":"+c2.getAge());
c2.eat();
System.out.println("==============");
//方式3
Animal c3 = new Cat();
c3.setName("大喵");
c3.setAge(3);
System.out.println(c3.getName()+":"+c3.getAge());
c3.eat();
System.out.println("==============");
//方式4
Animal c4 = new Cat("Kitty",4);
System.out.println(c4.getName()+":"+c4.getAge());
c4.eat();
}
}
复制代码
作者:
micro_hx
时间:
2015-6-29 23:07
对于实现类,在构造器中,我没有写supper方法,会不会报错啊???
作者:
Kevin.Kang
时间:
2015-6-30 10:11
//老师类练习
abstract class Person
{
private String name;
private int age;
public Person(){}
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public abstract void work();
}
class BasicTeacher extends Person
{
public BasicTeacher(){}
public BasicTeacher(String name, int age)
{
super(name,age);
}
public void work()
{
System.out.println("讲解javaSE");
}
}
class WorkTeacher extends Person
{
public WorkTeacher(){}
public WorkTeacher(String name, int age)
{
super(name,age);
}
public void work()
{
System.out.println("讲解javaEE");
}
}
class AbstractTest2
{
public static void main (String [] args)
{
Person p = new BasicTeacher("刘意",40);
System.out.println(p.getName()+":"+p.getAge());
p.work();
Person p2 = new WorkTeacher("张孝祥",45);
System.out.println(p2.getName()+":"+p2.getAge());
p2.work();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-6-30 14:49
abstract class Student
{
private String name;
private int age;
private String grand;
public Student(){}
public Student(String name, int age, String grand)
{
this.name = name;
this.age = age;
this.grand = grand;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getGrand()
{
return grand;
}
public void setGrand(String grand)
{
this.grand = grand;
}
public abstract void study();
public void eat()
{
System.out.println("学习饿了,去吃饭");
}
}
class BasicStudent extends Student
{
public BasicStudent(){}
public BasicStudent(String name, int age, String grand)
{
super(name,age,grand);
}
public void study()
{
System.out.println("基础班学员学习JavaSE");
}
}
class WorkStudent extends Student
{
public WorkStudent(){}
public WorkStudent(String name, int age, String grand)
{
super(name,age,grand);
}
public void study()
{
System.out.println("就业班学员学习JavaEE");
}
}
class AbstractTest3
{
public static void main (String [] args)
{
Student s = new BasicStudent("李延旭",20,"基础班");
System.out.println(s.getName()+":"+s.getAge()+":"+s.getGrand());
s.study();
s.eat();
System.out.println("==============");
Student s2 = new WorkStudent("任兴亚",23,"就业班");
System.out.println(s2.getName()+":"+s2.getAge()+":"+s2.getGrand());
s2.study();
s2.eat();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-6-30 15:58
abstract class YuanGong
{
private String name;
private String id;
private int salary;
public YuanGong(){}
public YuanGong(String name, String id, int salary)
{
this.name = name;
this.id = id;
this.salary = salary;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public int getSalary()
{
return salary;
}
public void setSalary(int salary)
{
this.salary = salary;
}
public abstract void work();
}
class Worker extends YuanGong
{
public Worker(){}
public Worker(String name, String id, int salary)
{
super(name,id,salary);
}
public void work()
{
System.out.println("按照需求写代码");
}
}
class Manager extends YuanGong
{
private int money;
public Manager(){}
public Manager(String name, String id, int salary, int money)
{
super(name,id,salary);
this.money = money;
}
public int getMoney()
{
return money;
}
public void setMoney(int money)
{
this.money = money;
}
public void work()
{
System.out.println("跟客户谈需求");
}
}
class AbstractTest4
{
public static void main (String [] args)
{
YuanGong y = new Worker("小明","00002",15000);
System.out.println(y.getName()+":"+y.getId()+":"+y.getSalary());
y.work();
System.out.println("===================");
Manager y2 = new Manager("大明","00001",20000,10000);
System.out.println(y2.getName()+":"+y2.getId()+":"+y2.getSalary()+":"+y2.getMoney());
y2.work();
}
}
复制代码
作者:
为明天而奋斗
时间:
2015-6-30 17:18
感觉好难哦
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2