龙空技术网

《Java面向对象程序》——类(1)

LearningYard学苑 1622

前言:

此刻朋友们对“java程序的组成的”都比较注重,兄弟们都想要了解一些“java程序的组成的”的相关文章。那么小编在网摘上收集了一些关于“java程序的组成的””的相关资讯,希望各位老铁们能喜欢,大家快快来学习一下吧!

今天继续为大家带来《Java面向对象程序设计》的学习。今天的主要内容是类,类的声明,类体,成员变量和局部变量。

Today we continue to bring you the study of Java Object Oriented Programming. Today's main topics are classes, class declarations, class bodies, member variables and local variables.

01

面向对象语言

面向对象编程主要体现三个特性。

1.封装性:将数据和对数据的操作封装在一起,通过抽象,即从具体的实例中抽取共同的性质形成一般的概念,比如类的概念。

2.继承性:子类可以继承父类的属性和功能,既继承了父类具有的数据和数据上的操作,同时又可以增添子类独有的数据和数据上的操作。

3.多态性:有两种意义上的多态,一种是操作名称的多态,即有多个操作具有相同的名字,但这些操作所接受的消息类型必须不同。另一种是多态是和继承有关的多态,是指同一个操作被不同类型对象调用时可能产生不同的行为。

Object-oriented programming embodies three main characteristics.

1. Encapsulation: data and operations on data encapsulated together, through abstraction, that is, from the concrete examples of common properties to form a general concept, such as the concept of class.

2. inheritance: subclasses can inherit the properties and functions of the parent class, both inherit the data and operations on the data that the parent class has, and at the same time can add the data and operations on the data unique to the subclass.

3. Polymorphism: There are two senses of polymorphism, one is the polymorphism of operation names, that is, there are multiple operations with the same name, but the types of messages accepted by these operations must be different. The other is polymorphism is polymorphism related to inheritance, which means that the same operation may produce different behaviors when called by different types of objects.

02

1.类

类是组成Java程序的基本要素,类封装了一类对象的状态和方法,类是用来定义对象的模板。

2.类的声明

class People和class动物被称为类的声明,People和动物分别是类名。类的名字要符合标识符的规定。

在给类命名时,要遵守以下编程风格:

(1)如果类名使用拉丁字母,那么名字的首字母使用大写字母,如Hello,Time和Dog等。

(2)类名最好容易识别,见名知意。当类名由几个“单词”复合而成时,每个单词的首字母使用大写,如Beijing和AmericanGame。

1. Classes

Classes are the basic elements of Java programs, classes encapsulate the state and methods of a class of objects, classes are used to define the object template.

2. Class declarations

class People and class animals are called class declarations, People and animals are class names respectively. The name of the class has to conform to the identifier.

When naming classes, the following programming style is observed.

(1) If the class name uses Latin letters, then the first letter of the name is in uppercase, such as Hello, Time and Dog.

(2) The class name is best easily recognized by its name. When the class name is a composite of several "words", use capital letters for the first letter of each word, such as Beijing and AmericanGame.

3.类体

写类的目的是为了描述一类事物共有的属性和功能,描述过程由类体实现。类体的内容由两部分构成:一部分是变量的声明,用来刻画属性;另一部分是方法的定义,用来刻画功能。

4.成员变量和局部变量

类体分为两部分:变量的声明部分和方法的定义。在变量声明部分声明的变量被称为类的成员变量,在方法体中声明的变量和方法的参数被称为局部变量。

(1)变量的类型

成员变量和局部变量的类型可以是Java中的任何一种数据类型。

(2)变量的有效范围

成员变量在整个类内部有效,局部变量只在声明它的方法内有效。方法参数在整个方法内有效,方法内的局部变量从声明它的位置之后开始有效。

(3)实例变量和类变量

成员变量又分为实例变量和类变量。在声明成员变量时,用关键字static给予修饰的变量称为类变量,否则称为实例变量(类变量也称为static变量,静态变量)。

3. Class body

The purpose of writing a class is to describe the properties and functions common to a class of things, and the description process is realized by the class body. The content of the class body consists of two parts: one is the declaration of variables, which is used to portray the properties; the other is the definition of methods, which is used to portray the functions.

4. Member variables and local variables

The class body is divided into two parts: the declaration part of the variables and the definition of the methods. The variables declared in the variable declaration part are called member variables of the class, and the variables declared in the method body and the parameters of the method are called local variables.

(1) Types of variables

The types of member variables and local variables can be any of the data types in Java.

(2) Valid scope of variables

Member variables are valid within the entire class, and local variables are valid only within the method in which they are declared. Method parameters are valid within the entire method, and local variables within the method are valid from the location where it is declared.

(3) Instance variables and class variables

Member variables are further divided into instance variables and class variables. When declaring a member variable, a variable modified with the keyword static is called a class variable, otherwise it is called an instance variable (class variables are also called static variables, static variables).

参考资料:文字:百度;图片:微博;翻译:百度翻译

本文由LearningYard新学苑原创,部分图片文字来自网络,如有侵权请联系。

标签: #java程序的组成的