Java中面向对象的三大基石— —— 封装

这周我们学习了Java中面向对象的三大基石之一    ——封装

1.封装的三大步骤:  ①  修改属性的可见性来限制对属性的访问。    (属性私有化)

                               ②   为每个属性创建一对赋值方法和取值方法,用来对这些属性的访问。(给属性创建get和set方法)

                               ③ 在赋值和取值方法中,加入对属性得到存取限制。  (给予限制和验证)

2. 重载的特点:

          ①  在同一个类中发生       ②  方法名共同      ③  参数列表不同     (同类同名不同参)

3. 构造方法:

       一:  构造方法是一个特殊的方法。Java中的每个类都有构造方法,用来初始化该类的一个对象。

      二:  构造方法名和类名相同,没有返回值类型。(没有void)

      三:  构造方法可以重载。

       四:  构造方法只能有new关键词和this() 调用,this() 只能在构造方法中使用,用来调用其他构造方法,且只能作为构造方法代码的第一条语句。

     如:     public Dog(){
this.mammal=true;     //是否是哺乳动物
this.canivorous=true;    // 是否食肉动物
this.numOfLegs=4;     //腿的条数
this.mood=1;   //心情

}    


写一下狗的属性来复习一下Java的封装:

1:

public class Dog {

//封装 
private boolean mammal;//是否是哺乳动物
private boolean canivorous;//是否是食肉动物
private int numOfLegs;//腿的条数
private int mood;//小狗的情绪

public boolean isMammal() {
return (mammal);
}
public void setMammal(boolean mammal) {
this.mammal = mammal;
}
public boolean isCanivorous() {
return canivorous;
}
public void setCanivorous(boolean canivorous) {
this.canivorous = canivorous;
}
public int getNumOfLegs() {
return numOfLegs;
}
public void setNumOfLegs(int numOfLegs) {
this.numOfLegs = numOfLegs;
}
public int getMood() {
return mood;
}
public void setMood(int mood) {
this.mood = mood;
}
//构造方法 设定初始值
public Dog(){
this.mammal=true;
this.canivorous=true;
this.numOfLegs=4;
this.mood=1;
}
public Dog(boolean mammal,boolean canivorous,int numOfLegs,int mood ){
this.mammal=true;
this.canivorous=true;
this.numOfLegs=4;
this.mood=2;
      }
//重载  构造方法(无参数的构造方法)
public String sayHello(){
return("摇摇尾巴!");
}
//有参数的构造方法
public String sayHello(int mood){
this.setMood(mood);
switch(this.mood){
case 1 ://开心
return"旺旺";
case 2 ://伤心
return"呜呜";
default:
return "摇摇尾巴!";
}
}

  }

2.狗的测试类:

public class TestDog {
      public static void main(String[] args) {
Dog xiaobai=new Dog();
if(xiaobai.isMammal()==true){
System.out.println("狗是哺乳动物!");
}else{
System.out.println("狗不是哺乳动物!");
}
if(xiaobai.isCanivorous()==true){
System.out.println("狗是肉食动物!");
}else{
System.out.println("狗不是肉食动物!");
}
System.out.println("一般情况下,狗打招呼的方式是"+xiaobai.sayHello());
System.out.println("狗高兴时候打招呼的方法是:"+xiaobai.sayHello(1));
System.out.println("狗伤心时候打招呼的方法是:"+xiaobai.sayHello(2));
System.out.println("狗有"+xiaobai.getNumOfLegs()+"条腿!");

System.out.println("------------以下是第二只狗-----------");
Dog xiaohei=new Dog(true,true,4,2);
if(xiaohei.isMammal()==true){
System.out.println("狗是哺乳动物!");
}else{
System.out.println("狗不是哺乳动物!");
}
if(xiaohei.isCanivorous()==true){
System.out.println("狗是肉食动物!");
}else{
System.out.println("狗不是肉食动物!");
}
System.out.println("一般情况下,狗打招呼的方式是"+xiaohei.sayHello());
System.out.println("狗高兴时候打招呼的方法是:"+xiaohei.sayHello(1));
System.out.println("狗伤心时候打招呼的方法是:"+xiaohei.sayHello(2));
System.out.println("狗有"+xiaohei.getNumOfLegs()+"条腿!");
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值