构造方法,传参,实参形参的练习

人送花的练习?
public class People {
String name;//姓名
int age;//年龄
//Shu s;//书
Flower f;//花

public People() {
	super();
}

public People(String name, int age, Flower f) {
	super();
	this.name = name;
	this.age = age;
	this.f = f;
}
public void lendHua(People p){//p是形参改变对p的实参没有影响的

	p.f=this.f;
	this.f=null;
	
}

public class Flower {
String collor;//颜色
String type;//种类
boolean isGoodSmell;

public Flower() {
	super();
}
public Flower(String collor, String type, boolean isGoodSmell) {
	super();
	this.collor = collor;//this指代当前调用的对象
	this.type = type;
	this.isGoodSmell = isGoodSmell;
}
public  void makeHappy(){
	System.out.println("很开心");
}
public void makeBeautiful(){
	System.out.println("装饰");
	}
}



public class TestFlower {

public static void main(String[] args) {
	
	Flower f=new Flower("红色", "玫瑰", false);
	People p1=new People("jack",22,f);
	People p2=new People("rose",22,f);
	
	System.out.println(p1.name+"有一个"+p1.f.type);//加了type就会空指针异常因为这时他没有花了
	System.out.println(p2.name+"有一个"+p2.f.type);
	
	System.out.println("送花后:");
	p1.lendHua(p2);//形参的改变对实参没有影响
	
	System.out.println(p2.name+"有一叫"+p2.f.type);
	System.out.println(p1.name+"有一叫"+p1.f);
}

}

人借钱的练习?
下面展示一些 内联代码片

public class Person {
String name;//姓名
double balance;//钱
public Person(String name, double balance) {
super();
this.name = name;
this.balance = balance;
}
public Person() {
super();
}

@Override
public String toString() {
	return "Person [name=" + name + ", balance=" + balance + "]";
}
//借钱
public void lendMoney(Person p,double money){
	this.balance-=20;
	p.balance+=20;
}

public class Test {

public static void main(String[] args) {
	Person p1=new Person("张三",100.0);
	System.out.println(p1.toString());
	Person p2=new Person("李四",10.0);
	System.out.println(p2.toString());
	
	p1.lendMoney(p2, 20);
	
	System.out.println("借钱完成");
	
	System.out.println(p1.toString());
	System.out.println(p2.toString());
}

}


狗咬人的练习?
public class Dog {
	public String name;// 姓名
	public char sex;// 性别

	public Dog(String name, char sex) {
		super();
		this.name = name;
		this.sex = sex;
	}

	public Dog() {
		super();
	}

	public void showData() {
		System.out.println("姓名:" + this.name + ",性别:" + this.sex);
	}

	public boolean Love(Dog d){
		if(this.sex==d.sex){
			return false;
		}else{
			return true;
		}
	}
	
	public void YaoRen(String str){
		if("男人".equals(str)){
			System.out.println("唬唬");	
		}else if("女人".equals(str)){
			System.out.println("喔喔");
		}else{
			System.out.println("哇哇");
		}
	}
}


public class Demo {

	public static void main(String[] args) {
		Dog d1 = new Dog("大黄", '公');

		Dog d2 = new Dog("花花", '母');
		boolean a = d1.Love(d2);
		if (a == true) {
			System.out.println("配对成功");
		} else {
			System.out.println("配对失败");
		}
		d1.YaoRen("男人");
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值