cs61b学习笔记 | Week1

class content: 

一、java入门

  ①hello world

        1.所有java代码都必须在class里

        2.语句以;结尾

        3.有符号  {  和  } 来限制一个代码区域

        4.要有 public static void main(String[].args){} 才能使程序开始运行

public class HelloWorld{
    public static void main(String[] args){
            System.out.println("hello wrold");
        }
}

  ②hello numbers

        1.声明变量、赋值变量

        2.已声明变量的类型不能重声明

public class HelloNumbers{
    public static void main(String[].args){
            int x=0;
            while(x<0){
                System.out.println(x);
                x=x+1;
            }
        }
   }              

  ③largerdemo

        1.main中引用方法需要在同一个class下声明 public static

        2.方法声明如果需要输入变量则需要于括号接口内声明变量类型,以及方法返回值也相似

public class Largerdemo{
    /** Return the larger of x and y.
        @cs61b*/
    public static int larger(int x,int y){
            if(x>y){
                return x;
                }
            return y;
        }           
    public static void main(String[].args){
           System.out.println(larger(-5,10));
        }
   }    

        静态typing的思考

The Good:

        1.可以捕捉具体的类型错误

        2.大概率不会出现类型错误在终端用户的电脑上

        3.更易读

        4.代码更高效率的运行,不用另外时间来检查类型

The Bad:

        1.代码会更冗长

        2.代码会不那么兼容(多类型)有方法在Java里(generics)

-----------------------------------------------------------------------------------------------------

二、git操作

  ①简单操作码      

       

        javac:编译.java文件生成 .class(运行必须文件)

        java:+java文件名(不带.class后缀)(e.g in image

        cat:+.java可以打印代码/+.class打印编译代码

        rm: 删除(remove)

        没有public static void main 不会java运行成功;

  三、class的简单指南

        ①.引用

        在另一个程序上测试运行(e.g: LargerLaucher(测试上述Largerdemo程序))

        把引用方法前的public改成对应的class名就行(如下例子中的LargerDemo)

public class Largerlauncher{         
    public static void main(String[].args){
           System.out.println(LargerDemo.larger(-5,10));
        }
   }   

       ②.数据

        class不仅可以存储方法,还可以存储数据

        如需引用,可以如同定义方法一样定义变量 (public int ....)

          (普通型不能用在static型里,具体如下)

        *.含数据输入的class(类与对象)

        

public class Dog {
    public int weightInPounds;                /**instance variable*/
 
    public Dog(int startingWeight) {         /**constructor (class参数的一种接入)*/
        weightInPounds = startingWeight;
        }
 
    public void makeNoise() {                /**非static方法(instance method)*/
        if (weightInPounds < 10) {
            System.out.println("yipyipyip!");
   		} else if (weightInPounds < 30) {
      	System.out.println("bark. bark.");
   	} else {
      	System.out.println("woof!");
   	}
   }
}
public class DogLauncher {
    public static void main(String[] args) {
    Dog smallDog;
    new Dog(20);
   		smallDog = new Dog(5);
   	Dog hugeDog = new Dog(150);
   		smallDog.makeNoise();
   	hugeDog.makeNoise();
}
}

      ③.static vs. Non-static

          *.static 方法可以用class名字前缀来激活,e.g. Dog.makeNoise();

          *.Instance mothods(Non-static)只能用instance名字前缀来激活,e.g. maya.makeNoise();(maya is an instance “Dog”)

          *.static 方法不能通过处理“my”instance variable,e.g. this.makeNoise();Dog原定义中

------------

          *.一个class可以有多个static和non-static的成员(variable/method) 

          *.同名member可以有static和non-static不同类型

      ④.对象的数据输入

          *.用直接赋值法(利用上述描述的Dog 类说明)

Dog tommmy = new Dog();
tommy.weightInPounds=10;

           *.含数据输入的对象创立

                 (见上述三.②代码部分)

      ⑤.this 的用法

                在non-static方法中可以使用this.blablabla来引用此instance中的方法或者变量

        

  四、String[] args用法       

          *简单阐述输入string

public class ArgsDemo {
	/** Prints out the 0th command line argument. */
	public static void main(String[] args) {
    	System.out.println(args[0]);
	}
}

         用 java ArgsDemo hello some args  -->可打印出  hello

      共有多个字母被输入进main中"hello some args" 

args[0]="hello"; 

args[1]="some";

args[2]="args"

args[]同其他数组一样也有constructor,附带args.length等数值(指代有多少成员)  

          *string 和 int 的转换

        利用Interger.parseint(String[]);

public class ArgsSum {
	/** Prints out the sum of arguments, assuming they are
 	*  integers.
 	*/
	public static void main(String[] args) {
    	int index = 0;
    	int sum = 0;
    	while (index < args.length) {
        	sum = sum + Integer.parseInt(args[index]);
        	index = index + 1;
    	}
    	System.out.println(sum);
	}
}

 ——————————————————————————————

after class:

Discussion:简单阐述java的一些细节问题,基础和我一样一般的可以看看

lab 1、2 :配置java环境和git简单操作        

hw0:循环 条件选择 数组 的基础知识和练习

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值