java第九章

基本要素

java类:分为data field和methods

UML(建模语言):

java第九章_第1张图片

data field(数据域):在class中,但不在method中,是变量的一种。

class&object

对象(object):现实生活中的可以区分开的实体。有着独特标识,静态属性(state,一组数据域)和动态属性(behavior,一组方法)。

An object represents an entity in the real world that can be distinctly identified.

class(类):一组同种类型的对象,对象是类里的一个实例。可构造对象开辟内存空间。可作为data type。包含data field,构造方法,方法。

 A class is a construct that defines objects of the same type.

两者关系:An object is an instance of a class.

A constructor is invoked to create an object.

测试

You can always use the default constructor even though the non-default constructors are defined in the class.(false)

The default constructor has no arguments.

Java assigns a default value to a local variable in a method if the variable is not initialized.(false)

Java assigns a default value to a data member of a class if the data is not initialized.

The default value for a data member of boolean type is true.(false)

The default value for data field of a boolean type, numeric type, object type is false,0,null, respectively.

You use the ___.___ operator to access members of an object.

A static variable can be accessed from any static method in the class.

Variables that are shared by every instances of a class are class variables.

A method that is associated with an individual object is called an instance method.

You can access a class variable using a syntax like objectName.classVariable or ClassName.classVariable.

A static method in a class can access the instance variables in the same class.(false)

A static data field can be accessed from any method in the same class.

You cannot use modifiers on local variables inside a method except final.

You cannot use the private modifier on classes.

To prevent a class from being instantiated,use the private modifier on the constructor.

Suppose you declare Date d. d is now called a reference variable for an object.

When invoking a method with an object argument, the reference of the object is passed.

Array variables are reference variables.

An immutable class cannot have public data fields.

The internal state of an immutable class cannot be changed. String is an immutable class.

You can declare two variables with the same name in different methods in a class.

ou can declare variables of the same name in a method even though they are in the same block.(false)

You can declare variables of the same name in a method if they are in non-nesting blocks.

You can declare a local variable in a method that has same name as an instance variable in the class.

Java uses this to reference the current object.

An instance data field f in the class can be referenced using this.f in an instance method in the same class.

An instance data field f in the class can be referenced using this.f in a static method in the same class.(false)

创建class

可以在一个包里创建多个class,但至少有一个是

构造方法:与方法同名,仅在创建时才被调用。不用return,直接改变了地址中的量。

( . ):对象成员引用操作符,格式:objectRefVar.methodName(arguments)。

引用变量:创造一个对象的地址,与数组类似。

Circle myCircle;//声明,定义myCircle的类型
myCircle = new Circle();//把引用名与对象关联

note:如果跟对象发生关系,不能用类名直接调用类里的方法,一定要用引用变量。

static:

默认值
reference type numeric type boolean type char type
null 0 false '\u0000'

垃圾回收机制(garbage collection):无名的对象就是垃圾,会占用内存空间,要尽快清理,java可以自动回收也能主动收回。

题目:

A object represents an entity in the real world that can be distinctly identified.

 A class is a construct that defines objects of the same type.

An object is an instance of a class.

A constructor is invoked to create an object.

You can not always use the default constructor even though the non-default constructors are defined in the class.

The default constructor has no arguments.

Java assigns a default value to a data member of a class if the data is not initialized.

Java doesn't assign a default value to a local variable in a method if the variable is not initialized.

The default value for a data member of boolean type is false.

The default value for data field of a boolean type, numeric type, object type is false,0,null, respectively.

You use the . operator to access members of an object.

A static variable can be accessed from any static method in the class.

Variables that are shared by every instances of a class are class variables

A method that is associated with an individual object is called an instance method.

You can access a class variable using a syntax like objectName.classVariable or ClassName.classVariable.

A static method in a class can't access the instance variables in the same class.

A static data field can't be accessed from any method in the same class.

You cannot use modifiers on local variables inside a method except final.

You cannot use the private modifier on classes.

To prevent a class from being instantiated,use the private modifier on the constructor.

Suppose you declare Date d. d is now called a reference variable for an object

假设您声明了Date d, d现在被称为对象的引用变量

When invoking a method with an object argument, the reference of the object is passed.

当调用带有对象参数的方法时,将传递对象的引用。

Array variables are reference variables.

Given the declaration Circle[] x = new Circle[10],x contains a reference to an array and each element in the array can hold a reference to a Circle object.

An immutable class cannot have public data fields

The internal state of an immutable class cannot be changed.

不可变类的内部状态不能更改

String is an immutable class.

String是一个不可变类。

All properties of an immutable object must be of primitive types is false about an immutable object

不可变对象的所有属性必须是基元类型,这对于不可变对象来说是假的

You can declare two variables with the same name in different methods in a class

可以在一个类的不同方法中声明两个名称相同的变量

You can't declare variables of the same name in a method even though they are in the same block.

你不能在一个方法中声明同名的变量,即使它们在同一个块中。

You can declare variables of the same name in a method if they are in non-nesting blocks.

如果方法位于非嵌套块中,可以在方法中声明名称相同的变量。

You can declare a local variable in a method that has same name as an instance variable in the class.

可以在方法中声明与类中的实例变量同名的局部变量。

Java uses this to reference the current object.

An instance data field f in the class can be referenced using this.f in an instance method in the same class.

An instance data field f in the class can't be referenced using this.f in a static method in the same class.

类中的实例数据字段f不能在同一个类的静态方法中使用this.f引用。

你可能感兴趣的:(java,开发语言,后端)