JAVA 分装

1、private私有化 只能在本类使用

2、提供公有方法来设置和获取属性值 getter setter

 

构造方法:

1、都是公共的

2、名字和类名一致

3、无返回值

4、不能写void

 

方法重载:

同名同类不同参(个数 类型 顺序)

 

代码如下:

public class Frog {

 

private boolean mammal=false;

private boolean canivorous=false;

private int numOflegs=4;

private int mood=1;

private boolean chanluan=true;

private boolean yousai=true;

public boolean isMammal(){

return(mammal);

}

public boolean isCarnivorous(){

return(canivorous);

}

public int getNumberOflegs(){

return (numOflegs);

}

public boolean isChanluan(){

return(chanluan);

}

public boolean isYousai(){

return(yousai);

}

public String sayHello(String sayHello){

return("呱呱呱");

}

public void setMood(int mood){

this.mood=mood;

}

public String sayHello(){

return("呱呱");

}

public String sayHello(int mood){

this.setMood(mood);

switch(this.mood){

case 1:

return("呱呱呱");

case 2:

return("扑腾一声跳入水中");

default:

return("呱呱");

}

}

 

public Frog(boolean mammal,boolean carnivorous,int numOfLegs,int mood,boolean luansheng,boolean yousai){

this.mammal=mammal;

this.canivorous=canivorous;

this.numOflegs=numOfLegs;

this.mood=1;

this.chanluan=luansheng;

this.yousai=yousai;

}

}

 

 

 

 

public class Frogtext {

public static void main(String[] args){

Frog frog1=new Frog(false,false,4,1,true,true);

if(frog1.isMammal()){

System.out.println("青蛙是哺乳动物");

}else{

System.out.println("青蛙不是哺乳动物");

}

if(frog1.isCarnivorous()){

System.out.println("青蛙是肉食动物");

}else{

System.out.println("青蛙不是肉食动物");

}

System.out.println("青蛙通常的情况下,和人打招呼的方式为:"+frog1.sayHello());

System.out.println("青蛙情绪好的情况:"+frog1.sayHello(1));

System.out.println("青蛙受到惊吓的时候会:"+frog1.sayHello(2));

System.out.println("青蛙有几条腿:"+frog1.getNumberOflegs()+"条腿");

if(frog1.isYousai()){

System.out.println("青蛙有腮");

}else{

System.out.println("青蛙无腮");

}

if(frog1.isChanluan()){

System.out.println("青蛙产卵");

}else{

System.out.println("青蛙不产卵");

}

 

}

 

}

转载于:https://my.oschina.net/u/3820389/blog/1807820

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值