人送花的练习?
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("男人");
}
}