标准格式包含: 私有属性 无参构造 有参构造 setter 和getter 需求中的方法 需求一: 员工类Employee 属性:姓名name,工号id,工资salary 行为:显示所有成员信息的方法show() 需求二: 动物类Animal 属性:姓名name,年龄age 行为:吃饭

 

 

 1 // 员工类
 2 
 3 public class Employee {
 4     private String name;
 5     private int id;
 6     private double salary;
 7 
 8     public void show() {
 9         System.out.println(name+"\t"+id+"\t"+salary);
10     }
11     public Employee() {
12 
13     }
14     public Employee(String name, int id, double salary) {
15         this.name = name;
16         this.id = id;
17         this.salary = salary;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public int getId() {
26         return id;
27     }
28     public void setId(int id) {
29         this.id = id;
30     }
31     public double getSalary() {
32         return salary;
33     }
34     public void setSalary(double salary) {
35         this.salary = salary;
36     }
37 }
 1 // 动物类
 2 public class Animal {
 3     private String name;
 4     private int age;
 5 
 6     public Animal() {
 7         
 8     }
 9     public Animal(String name, int age) {
10         this.name = name;
11         this.age = age;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public int getAge() {
20         return age;
21     }
22     public void setAge(int age) {
23         this.age = age;
24     }
25     public void eat() {
26         System.out.println("吃饭");
27     }
28     public void sleep() {
29         System.out.println("睡觉");
30     }
31 }
 1 //人类
 2 
 3 public class Person {
 4     private String name;
 5     private int age;
 6     private String gender;
 7 
 8     public Person() {
 9         
10     }
11     public Person(String name, int age, String gender) {
12         this.name = name;
13         this.age = age;
14         this.gender = gender;
15     }
16     
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public int getAge() {
26         return age;
27     }
28 
29     public void setAge(int age) {
30         this.age = age;
31     }
32 
33     public String getGender() {
34         return gender;
35     }
36 
37     public void setGender(String gender) {
38         this.gender = gender;
39     }
40 
41     public void study() {
42         System.out.println("学习");
43     }
44 
45     public void sleep() {
46         System.out.println("睡觉");
47     }
48 }
 1 //测试类
 2 
 3 public class TestA {
 4     public static void main(String[] args) {
 5 
 6         Animal a = new Animal("藏獒",50);
 7         System.out.println(a.getName());
 8         System.out.println(a.getAge());
 9         a.eat();
10         a.sleep();
11         System.out.println("============================");
12         Employee e = new Employee("张三",20190316,20);
13         e.show();
14         System.out.println("============================");
15         Person p = new Person("李四",30,"男");
16         System.out.println(p.getName());
17         System.out.println(p.getAge());
18         System.out.println(p.getGender());
19         p.study();
20         p.sleep();
21     }
22 }

 

posted @ 2019-03-17 23:04  Java练习生  阅读(3404)  评论(0编辑  收藏  举报