Java学习笔记小结

时间:02.28——02.29

28日

对象的行为:
状态影响行为,行为影响状态。
类——>对象
状态——>实例变量;
行为——>方法。
研究对象就是在分析它的状态和行为。
在这里插入图片描述

在这里插入图片描述

通过对实例变量的限制来达到封装的目的。

class Dog{
    String name;
    int size;
    void setname(String s){
        name=s;
    }
    void setsize(int x){
        if(size>9)//封装(应该)
        size=x;
    }
    String getname(){
        return name;
    }
    int getsize{
        return size;
    }
}

对象的状态(实例变量)影响了它的行为(方法)。

class Dog{
 int size;
 String name;
 void bark(){//记得加()
 if(size>60) {System.out.println("woof! woof!");
 }else if(size>20){System.out.println("Ruff! Ruff!");
 }else {System.out.println("Yip! Yip!");
 }
 }
 }

实例变量永远都会有默认值,即使没有明确的赋值给实例变量,或者没有调用setter,实例变量还是会有值:
例如:boolean:false;
String :null;

实例变量和局部变量之间的差距:
1.实例变量声明在类中而不是方法中。
2.局部变量使用前需要初始化(局部变量没有初始值,如果初始化前使用的话编译器会报错)。
3.局部变量的传递的参数的改变不会影响到实例变量。

1.使用“= =”来判断两个引用是否指向同一个对象
2.使用“= =”比对primitive主数据类型

int a=3;
byte b=3;
if(a==b){//只比较字节组合
    true;
}

int guess=Integer.parselent(stringGuess);//将字符串转换成int;

for循环:就是括号里的语句每完全执行完一次,cell的值都会变为cell数组的下一个数字,直到跳出循环或循环结束为止。

for(int cell:locationCells){
    if(guess==cell){
        result="hit";
        numofHit++;
        break;
    }
}

29日

经过昨天的简单学习,我今天正式开始写这个游戏SimpleDotCom
这个游戏需要创建两个类:SimpleDotCom类,SimpleDotComGame类。
在这里插入图片描述
simpleDotCom类作用:设置locationCells()表示目标值,通过被不断调用checkyourself()来达到目的。

public class SimpleDotCom{
int [] locationCells;
int numOfHits=0;
public void setLocationCells(int[] args){
locationCells=args;
}
public String checkyourself(String stringGuess){
int Guess=Integer.parseInt(stringGuess);
String result="miss";
for(int cell=locationCells){
if(Guess==cell){
result="Hit";
numOfHits++;
break;
}
}
if(numOfHits==locationCells.length){
result="Kill";
}
System.out.println(result);
return result;
}
}

int rand1=(int) (Math.random()*x);//随机产生x以内的随机数字
(int)强制转换,因为随机出现的的数字可能会是double类型。

SimpleDotCom完整代码:

public class SimpleDotCom{
int [] locationCells;
int numOfHits=0;
public void setLocationCells(int[] args){
locationCells=args;
}
public String checkyourself(String stringGuess){
int Guess=Integer.parseInt(stringGuess);
String result="miss";
for(int cell:locationCells){
if(Guess==cell) {
result="Hit";
numOfHits++;
break;
}
}
if(numOfHits==locationCells.length){
result="Kill";
}
System.out.println(result);
return result;
}
}
class SimpleDotComGame{ 
public static void main(String[] args){
	int numOfGuess=0;
	GameHelper helper=new GameHelper();
	SimpleDotCom theDotCom=new SimpleDotCom();
	int randomNum=(int) (Math.random()*5);
	int[] locations={randomNum,randomNum+1,randomNum+2};
	theDotCom.setLocationCells(locations);
	boolean isAlive=true;
	while(isAlive==true){
		String guess=helper.getUserInput("enter a number");
		String result =theDotCom.checkyourself(guess);
		numOfGuess++;
		if(result.equals("Kill")){
			isAlive=false;
			System.out.println("You took "+numOfGuess +" guesses");
		}
	}
}
}
public class GameHelper{
	public String getUserInput(String prompt){
String inputLine=null;
System.out.print(prompt+" ");
try{
BufferedReader is =new BufferedReader(new InputStreamReader(System.in));
inputLine=is.readLine();
if(inputLine.length()==0) return null;
}catch(IOException e){
System.out.println("IOException: "+ e);
}
return inputLine;
}
}

这个程序其实还是无法运行,具体为什么可能还要等我以后更深入的学习。但是关于连续输入一个同一个数三次就会输出Kill的bug已经在作者的教导下改了出来:

import java.util.ArrayList;
public class SimpleDotCom{
private ArrayList locationCells;
//int numOfHits=0;
public void setLocationCells(ArrayList loc){
locationCells=loc;
}
public String checkyourself(String stringGuess){
int Guess=locationCells.indexOf(stringGuess);
String result="miss";
if(Guess>=0){
locationCells.remove(Guess);
if(locationCells.isEmpty()){
result="Kill";
}else{
result="Hit";
}
}
if(numOfHits==locationCells.length){
result="Kill";
}
System.out.println(result);
return result;
}
}

总结:关于Java的学习,关键还是在于对面向对象的理解。通过对对象的分析,了解它的状态和行为由此不断改进和调整。

函数库

Java的内置有数百个类。
ArrayList类:

add(Object elem)//向list中加入对象参数
remove(int index)//在索引参数中移除对象
remove(Object elem)//移除该对象
contains(Object elem)//如果和对象参数匹配返回“true”
isEmpty()//如果list中没有元素返回“true”
indexOf(Object elem)//返回对象参数的索引或-1
size()//返回list中元素的一个数
get(int index)//返回当前索引参数的对象

以上只是Array类的部分样本,实际更为复杂。

ArrayList myList=new ArrayList();//创建

Egg s=new Egg();
myList.add(s);//加入元素

Egg b=new Egg();
myList.add(b);//再加入元素

int theSize=myList.size();//查询大小

boolean isIn=myList.contains(s);//查询特定元素

int idx=myList.indexOf(b);//查询特定元素位置

boolean empty=myList.isEmpty();//判断集合是否为空

myList.removes(s);//删除元素

L1:<>表示创建出来的是egg类型的list;
L11:返回true;
L13:Array是零基,b是第二个元素,所以会返回1;
L15:因为list非空,所以会返回false。

比较一般数组和Array对象:
1.一般数组在创建时就必须确定大小
2.存放对象给数组时必须指定对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值