CS61b LECTURE 3 NOTE

2023-11-06 17:10
文章标签 note lecture cs61b

本文主要是介绍CS61b LECTURE 3 NOTE,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • CS61b LECTURE 3
    • defining classes (定义类)
      • Defining Classes
      • Constructors
        • The "this" Keyword
        • The "static" Keyword
        • Lifetimes of Variables

CS61b LECTURE 3

defining classes (定义类)

Defining Classes

Constructors

The “this” Keyword

A method invocation implicitly passes an object as a parameter called “this”.

The “static” Keyword

A static_field is a single variable shared by a whole class of objects; its value does not vary from object to object.
If we declare a field “static”, there is just one field for the whole class. Static fields are also called class_variables
System.in and System.out are other example of static fields.
Methods can be static too. A static method does not implicitly pass an object as a parameter.
The main method is always static.

Lifetimes of Variables
  • A local variable (declared in a method) is gone forever as soon as the method in which it is declared finished executing.(If it references an object, the object might continue to exist, though.)
  • An instance variable (non-static field) lasts as long as the object, the object lasts as long as there is a reference to it.
  • A class variable (static field) lasts as long as the program runs.

An object is a repository of data.
Fields are variables that hold the data stored in objects. ( instance_variables)

class Human{public static int numberOfHumans;public static void printHumans() {System.out.println(numberOfHumans);}// call Human.printHumans()public int age;public String name;public void introduce() {System.out.println("I' m " + name + " and I' m " + age + " years old.");}public void copy(Human original) {age = original.age;name = original.name;}// copy original's fields into the object who calls the original// Constructors// A constructor is a method that constructs a Human. // The constructor won't actually contain code that dose the creatingpublic Human(String givenName) {numberOfHuman++;age = 6;name = givenName;}// A constructor// Java provides every class with a default constructor, which takes no parameters and does no initializing.// Warning: Once written your own constructor, the default constructor goes away. If you still want a default constructor, you must define it explicitly like below:public Human() {numberOfHumans++;}// override the default constructor public Human() {numberOfHumans++;age = 0;name = "Untitiled";}// Using "this" Keyword but DOES the SAME thing as abovepublic Human() {numberOfHumans++;this.age = 0;this.name = "Untitiled";}// if the parameters (local variables) of a method have the same name as the fields of an object, then the former have priority, and the "this Keyword" is needed to refer to the object's fieldspublic void change(int age) {String name = "Tom";this.age = age;this.name = name;
}
  • construct 一个 Human对象
Human amanda = new Human(); // create amanda
amanda.age = 6; //set amanda's fields
amanda.name = "Amanda";
amanda.introduce();  //_Method_call
// Using the constructor:
Human amanda = new Human("Amanda");
amanda.introduce();
// look at the variable numberOfHuman, prefix it with the class name rather than the name of a specific object.
int kids = Human.numberOfHumans / 4;
                --------------|      ----  |---    |  age | 6|  |amanda |.+--->|      ----  |     ---------------    | name | -+--+---->| "Amanda" ||      ----  |     --------------------------    a String objecta Human object

在这里插入图片描述

这篇关于CS61b LECTURE 3 NOTE的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/357961

相关文章

(南京观海微电子)——GH7006 Application Note

Features ⚫ Single chip solution for a WXGA α-Si type LCD display ⚫ Integrate 1200 channel source driver and timing controller ⚫ Display Resolution: ◼ 800 RGB x 480 ◼ 640 RGB x 480 ⚫ Display int

chapter06 面向对象基础 知识点Note

文章目录 前言类的设计 属性和行为对象的内存解析 (堆 栈 方法区)类的成员之一 变量(属性) field类的成员之二 方法 method对象数组方法重载 overload可变个数的形参 语法糖方法的值传递机制递归关键字package importMVC设计模式import导入面向对象特征之一 封装类的成员之三 构造器JavaBeanUML类图 前言 ` 面向对象封装 面向

Cmake note

cmake 指定交叉编译工具 指定install安装目录 $CC=arm-linux-uclibcgnueabi-gcc cmake -DCMAKE_INSTALL_PREFIX=./output . $make $make install 删除camke cache文件: find . -iname ‘cmake’ -not -name CMakeLists.txt -exec rm -rf

chapter01 Java语言概述 知识点Note

JavaSE JavaEE JavaME 大数据 Java基础常用技术栈 mysql JDBC SSM spring+spring mvc+mybatis Linux nacos Hadoop Flink JAVA EE 消息队列 rabbitMQ docker 数据库 redis spring boot springcloud ssh struts + spring + hiber

chapter03 流程语句 知识点Note

@TOC 分支结构if-else 和 switch-case switch(表达式){case 常量值1:语句块1;//break;case 常量值2:语句块2;//break; // ...[default:语句块n+1;break;]} switch-case 执行过程: 第1步:根据switch中表达式的值,依次匹配各个case。如果表达式的值等于某个case中的常量值,则执行对

work note

1:  sum_total 是什么意思?  没有百度出来 见proce:  rptMohthCdr

note-Redis实战3 核心-数据安全与性能保障

助记提要 快照持久化的作用和缺点Redis创建快照的时机AOF文件同步的三种配置AOF文件重写的方式Redis复制的配置项和控制命令Redis复制过程 5步Redis主从链确认数据写入从服务器硬盘故障处理的两步Redis事务命令 5个Redis事务的特点 3点非事务型流水线使用性能测试工具评估客户端的性能 4章 数据安全与性能保障 持久化和复制 故障恢复 事务和流水线 4.1 快照持久

study note of CCS

Notes of DSP learning 每个CCS的project工程都包括哪些东西: Src:每个Project里面会有一个src的文件夹,这个文件夹里面是一些.c和.asm的文件,个人理解就是一些函数的实现,自己写代码的时候调用的函数就是在这些.C的文件里面的,这些.C和.asm的文件可以在CCS的安装目录的\TI\controlSUITE\device_support\f2802x\v

vim note(4)

:new 文件名.后缀 新建文件。 :e 文件名 打开文件。 :w 文件名.txt  保存文件。 :wq 保存并退出。 :x 退出,如果文件更改则保存

三星Note3添加S Note笔记模板教程

本文介绍三星Note3使用技巧,即S Note如何添加S Note笔记模板的详细教程。N9006、N9008、N9002、N9009等版本都可参考本教程进行操作。Galaxy Note3内置的S Note可以添加笔记朱雀网络 www.zhuquewl.com ROM包下载模板,让笔记看起来更加美观和个性化。不知道怎么添加S Note笔记模板的话,请仔细看下面的具体操作方法: 1、在待机页面下,点击