案例总结

案例总结


 

public class Cat {
    //成员属性:昵称、年龄、体重、品种
	String name;//昵称 String类型默认值null
	int month;//年龄 int类型默认值0
	double weight;//体重 double类型默认值0.0
	String species;//品种

	public Cat(){
		System.out.println("我是无参构造方法");
	}
	public Cat(String name){
		System.out.println("我是带参构造方法");
	}
	public Cat(String name,int month,double weight,String species){
		this();
		this.name=name;
		this.month=month;
		this.weight=weight;
		this.species=species;
	}
	
//	public void Cat(){
//		System.out.println("我只是一个普通的方法,恰好方法名叫Cat");
//	}

	//成员方法:跑动、吃东西
	//跑动的方法
	public void run(){
//		Cat();
		eat();
		System.out.println("小猫快跑");
	}
	public void run(String name){
		System.out.println(name+"快跑");
	}
	//吃东西的方法
	public void eat(){
		System.out.println("小猫吃鱼");
	}
}
//单一职责原则
public class CatTest {
	public static void main(String[] args) {
		// 对象实例化
		Cat one = new Cat("花花",2,1000,"英国短毛猫");
	    // 测试
//		one.name = "花花";
//		one.month = 2;
//		one.weight = 1000;
//		one.species = "英国短毛猫";
		
		System.out.println("昵称:" + one.name);
		System.out.println("年龄:" + one.month);
		System.out.println("体重:" + one.weight);
		System.out.println("品种:" + one.species);
		one.run();
//		one.Cat();

	}
}

 
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值