Java学习手册:你的知识储备有多少?

发表时间: 2023-05-17 16:00

学习知多少——java篇.mp3" content="来自LearningYard学苑">

学习知多少——java篇.mp33:09
来自LearningYard学苑

分享兴趣,传播快乐,增长见闻,留下美好!

亲爱的您,这里是LearningYard学苑。今天小编为大家带来“学习知多少:java篇”,欢迎您的访问。

Share interests, spread happiness, increase knowledge, and leave a good legacy!

Dear you, this is The LearningYard Academy. Today Xiaobian brings you "Learn how much to know :Java chapter”, welcome your visit.

一、思维导图

一、 Mind mapping




二、创建对象

二、create object


对象是根据类创建的。在Java中,使用关键字 new 来创建一个新的对象。创建对象需要以下三步:

Objects are created based on classes. In Java, the keyword new is used to create a new object. Creating an object requires the following three steps:


声明:声明一个对象,包括对象名称和对象类型。

Declaration: Declare an object, including its name and type.


实例化:使用关键字 new 来创建一个对象。

Instantiation: Use the keyword new to create an object.


初始化:使用 new 创建对象时,会调用构造方法初始化对象。

Initialization: When creating an object using new, the construction method is called to initialize the object.


下面是一个创建对象的例子:

Here is an example of creating an object:


public class Puppy{ public Puppy(String name){ //这个构造器仅有一个参数:name System.out.println("小狗的名字是 : " + name ); } public static void main(String[] args){ // 下面的语句将创建一个Puppy对象 Puppy myPuppy = new Puppy( "tommy" ); }}


编译并运行上面的程序,会打印出下面的结果:

小狗的名字是 : tommy

Compiling and running the above program will print the following results:The name of the puppy is: tommy


访问实例变量和方法

Accessing instance variables and methods


通过已创建的对象来访问成员变量和成员方法,如下所示:

Access member variables and member methods through the created objects, as follows:


/* 实例化对象 */Object referenceVariable = new Constructor();/* 访问类中的变量 *
/referenceVariable.variableName;/* 访问类中的方法 *
/referenceVariable.methodName();

/*Instantiate object */Object referenceVariable=new Constructor()/* Accessing variables in a class */referenceVariable. variableName/* Accessing methods in the class */referenceVariable. methodName();


实例

下面的例子展示如何访问实例变量和调用成员方法:

example

The following example shows how to access instance variables and call member methods:


public class Puppy{ int puppyAge; public Puppy(String name){ // 这个构造器仅有一个参数:name System.out.println("小狗的名字是 : " + name ); } public void setAge( int age ){ puppyAge = age; } public int getAge( ){ System.out.println("小狗的年龄为 : " + puppyAge ); return puppyAge; } public static void main(String[] args){ /* 创建对象 */ Puppy myPuppy = new Puppy( "tommy" ); /* 通过方法来设定age */ myPuppy.setAge( 2 ); /* 调用另一个方法获取age */ myPuppy.getAge( ); /*你也可以像下面这样访问成员变量 */ System.out.println("变量值 : " + myPuppy.puppyAge ); }}


编译并运行上面的程序,产生如下结果:

小狗的名字是 : tommy小狗的年龄为 : 2变量值 :

Compile and run the above program, resulting in the following results:

The name of the puppy is: tommy. The age of the puppy is: 2. Variable value:





今天的分享就到这里了。
如果您对今天的文章有独特的想法,
欢迎给我们留言,
让我们相约明天。
祝您今天过得开心快乐!

That's all for today's sharing.
If you have a unique idea for today’s article,
please leave us a message,
and let us meet tomorrow.
I wish you a happy day !


本文由learningyard新学苑原创,如有侵权,请联系我们!

翻译来源于谷歌翻译

部分来源于

百度文库

清华大学出版《java程序设计基础》

编辑&排版|百味

审核|闫庆红