抽象和封装(一)

1.从现实抽象出类的步骤
  第一步:找出分类(分析出类)
  第二步:找出类的特征(分析类的相关属性)
  第三步:找出类的行为(分析类的方法)

2.常量(就是出现的变量值)
  语法:public static final 数据类型 常量名=常量值;
    eg:public static final String SEX_MALE="Q仔";
    eg:public static final String SEX_FEMALE="Q妹";
  注意:final是指最终的,修饰的变量不能更改
  常量名:所有的字母都要大写,多个单词用_分隔
  常量的访问:类名.常量名;
    eg: Penguin.SEX_MALE;

3.构造方法(特殊方法,用来创建对象)
  语法:public 类名(参数列表){
    //方法体
  }
  使用:类名 对象=new 构造方法名(参数);
    eg:Dog d=new Dog();
  作用:a.创建对象
     b.初始化值(属性值)


  构造方法和普通方法的区别:
    a.构造方法没有返回值类型(return...)
    b.构造方法的方法名是类名,普通方法可以任意取
    c.构造方法调用:new 构造方法名(参数)
      普通方法的调用:对象名.方法名(参数)
  注意:当没有写构造方法时,Java会提供默认的无参构造函数(构造方法)
  eg:public Dog(){

    }

4.方法重载:方法名一样,参数列表不一样
  注意:重载与返回值类型和访问修饰符无关

5.static 和final
  static:静态的
    用static修饰的属性,直接可以类名.方法名访问
  final:最终的
    用final修饰的属性,它的值初始化后,不能再改变
  注意:static不能再方法中修饰局部变量

6.封装
  6.1封装:隐藏类内部实现细节
  6.2步骤
    第一步:将属性私有化(private)
    第二步:提供getter/setter方法(getXxxx(),setXxxx())
    第三步:在getter/setter中加入控制语句
  6.3this关键词
    this:表示当前对象
    调用属性:this.属性名
    调用方法:this方法名()
    this():表示调用构造函数
    注意:this():必需写在构造函数的第一行

 

 1 package Day01一;
 2 
 3 public class Dog {
 4     int strain;                    //品种
 5     int age;                    //年龄
 6     String name;                //昵称
 7     int health;                    //健康值
 8     int love;                    //亲密度
 9     //语法:public 类名(参数列表){ }
10     //构造方法(默认构造方法)
11     public Dog(){
12         
13     }
14     public Dog(String name,int strain){
15         //初始化值
16         this();            //当前的构造函数
17         this.name=name;
18         this.strain=strain;
19     }
20     public Dog(String name,int age,int love,int health){
21         this.name=name;
22         this.age=age;
23         this.love=love;
24         this.health=health;
25         
26         System.out.println("----构造方法----");
27     }
28     //打印信息(this表示当前对象)
29     public void print(){
30         System.out.println("名字:"+this.name+",年龄:"+this.age+",品种:"
31             +this.strain+",健康值:"+this.health+",亲密度:"+this.love);
32     }
33     
34     
35     
36     public void setHealth(int health){
37         if(health>0 && health<100){
38             this.health=health;
39             return;
40         }
41         this.health=-1;
42     }
43     public int getHealth(){
44         return health;
45     }
46     
47     public void setName(String name){
48         this.name=name;
49     }
50     public String getName(){
51         return name;
52     }
53     
54     public void setLove(int love){
55         this.love=love;
56     }
57     public int getLove(){
58         return love;
59     }
60     
61     public void setAge(int age){
62         this.age=age;
63     }
64     public int getAge(){
65         return age;
66     }
67     
68     public void setStrain(int strain){
69         this.strain=strain;
70     }
71     public int getStrain(){
72         return strain;
73     }
74     
75 }
Dog类
 1 package Day01一;
 2 
 3 public class Penguin {
 4     String name="无名氏";        //昵称
 5     int health=100;                //健康值
 6     int love=0;                    //亲密度
 7     String sex="Q仔";            //性别
 8     //常量---变量
 9     //public static final 常量名=常量值;
10     public static final String SEX_MALE="Q仔";
11     public static final String SEX_FEMALE="Q妹";
12     //注意:final是指最终的,修饰的变量不能更改
13     //        常量名:所有的字母都要大写,多个单词用_分隔
14 
15     /**
16      * 输出企鹅的信息
17      */
18     public void print(){
19         System.out.println("宠物的自白:\n我的名字叫"+this.name+",健康值是"+this.health+",和主人的亲密度是"+
20                 this.love+",性别是"+this.sex+"。");
21     }
22 }
penguin类
 1 package Day01一;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test {
 6     public static void main(String[] args) {
 7         Scanner input=new Scanner(System.in);
 8         System.out.println("欢迎您来到宠物店!");
 9         System.out.println("请输入要领养的宠物的名字:");
10         String name=input.next();
11         System.out.println("请选择要领养的宠物类型:(1.狗狗2.企鹅):");
12         int type=input.nextInt();
13         int strain=0;
14         if(type==1){
15             System.out.println("请选择狗狗的品种:(1.聪明的拉布拉多犬2.酷酷的雪纳瑞):");
16             strain=input.nextInt();
17         }
18         int sex=0;
19         if(type==2){
20             System.out.println("请选择企鹅的性别:(1.Q仔2.Q妹):");
21             sex=input.nextInt();
22         }
23         
24         if(type==1){
25             Dog d=new Dog();
26             d.name=name;
27             d.strain=strain;
28             d.print();
29         }
30         if(type==2){
31             Penguin p=new Penguin();
32             p.name=name;
33 //            if(sex==1){
34 //                p.sex="Q仔";
35 //            }
36 //            if(sex==2){
37 //                p.sex="Q妹";
38 //            }
39             if(sex==1){
40                 p.sex=Penguin.SEX_MALE;
41             }
42             if(sex==2){
43                 p.sex=Penguin.SEX_FEMALE;
44             }
45             p.print();
46         }
47     }
48 }
测试类

 

posted @ 2017-06-15 20:48  眼泪,还是流了  阅读(299)  评论(0编辑  收藏  举报