java笔记——传递与this关键字

值传递与引用传递






对象的一对一关系

class Demo
{
	public static void main(String[] args)
	{
		Husband h = new Husband("小白",'男');
		Wife w = new Wife("小丽",19);
		//关联丈夫跟妻子的关系
		h.wife = w;
		w.husband = h;

		System.out.println("我的名字叫"+h.name+",性别:"+h.sex+",我有一个女朋友叫"+h.wife.name+",她今年"+h.wife.age+"岁");
	}
}
class Husband
{
	String name;
	char sex;
	Wife wife;//关联妻子类
	//getter../setter...省略.直接构造
	public Husband(){};
	public Husband(String name,char sex)
	{
		this.name = name;
		this.sex = sex;
	}
}
class Wife
{
	String name;
	int age;
	Husband husband;//关联丈夫类
	public Wife(){}
	public Wife(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
}

this关键字

在java基础中,this关键字是一个重要的概念,使用this关键字可以完成下列操作:

1、调用类中的属性

2、调用类中的方法或构造函数

3、表示当前对象

class Demo
{
	public static void main(String[] args)
	{
		Man m = new Man();
	}
}
class Man
{
	private String name;
	private int age;
	public Man()
	{
		this("小红");//this调用构造函数
	}
	public Man(String name)
	{
		this(name,18); 
	}
	public Man(String name,int age)
	{
		this.name = name;
		this.age = age;
		this.Say();//调用本类中的方法
	}
	void Say()
	{
		System.out.println("我叫"+name+",今年"+age+",岁");
	}
}

作业:

import java.util.Scanner;
import java.util.Random;
/**
	作业
	1、通过控制台命令方式实现一个猜拳游戏,
	用户通过输入(1.剪刀 2.石头 3.布),
	与电脑PK,最后通过积分的多少判定胜负。

*/
public class HomeWork1{
	public static void main(String[] args){
		Game g = new Game();
		g.begin();
	}
}
/**
	猜拳游戏类实现思路
	1、在控制台输出玩法提示
	2、是否开始游戏(接收1表示开始,0退出)
	3、接收游戏的局数
	4、循环接收用户的出拳(1.剪刀 2.石头 3.布)
	5、系统要随机出拳
	6、进行比较记录胜者次数
	7、公布结果
*/
class Game{
	public void begin(){
		System.out.println("*************************");
		System.out.println("*********猜拳游戏**********");
		System.out.println("游戏规则:(1.剪刀 2.石头 3.布)");
		System.out.println("开始游戏(1/0):");
		Scanner input = new Scanner(System.in);
		int result = input.nextInt();
		if(result==1){
			System.out.println("请输入猜拳次数:");
			int num = input.nextInt();
			play(num);
		}else{
			System.out.println("bye bye!");
		}
	}
	//游戏核心方法
	public void play(int num){
		int userScore = 0;//用户胜的局数
		int pcScore = 0;//电脑胜的局数
		Random r = new Random();
		Scanner input = new Scanner(System.in);
		while(num>0){
			//电脑出的拳
			int x = r.nextInt(100)%3+1;
			System.out.println("请输入你的选择(1,2,3):");
			int s = input.nextInt();
			if(s==1){
				switch(x){
					case 1:
						System.out.println("平局,你出剪刀,电脑出剪刀");
						break;
					case 2:
						System.out.println("你输了,你出剪刀,电脑出石头");
						pcScore++;
						break;
					case 3:
						System.out.println("你赢了,你出剪刀,电脑出布");
						userScore++;
						break;
				}
			}
			if(s==2){
				switch(x){
					case 1:
						System.out.println("你赢了,你出石头,电脑出剪刀");
						userScore++;
						break;
					case 2:
						System.out.println("平局,你出石头,电脑出石头");
						break;
					case 3:
						System.out.println("你输了,你出石头,电脑出布");
						pcScore++;
						break;
				}
			}
			if(s==3){
				switch(x){
					case 1:
						System.out.println("你输了,你出布,电脑出剪刀");
						pcScore++;
						break;
					case 2:
						System.out.println("你赢了,你出布,电脑出石头");
						userScore++;
						break;
					case 3:
						System.out.println("平局,你出布,电脑出布");
						break;
				}
			}
			num--;
		}
		System.out.println("***********************");
		System.out.println("你胜:"+userScore);
		System.out.println("电脑胜:"+pcScore);
		if(userScore==pcScore){
			System.out.println("平手");
		}else if(userScore>pcScore){
			System.out.println("你赢了");
		}else{
			System.out.println("电脑赢了");
		}
	}
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值