[JAVA] Getting Classes and Objects

First, the definition of the object class

What is class? What is an object?

类:对现实生活中一类具有共同特征的事物的抽象。
对象:对象就是客观世界中存在的实体在计算机逻辑中的映射。

Second, the relationship between classes and objects

Object is a specific class. E.g:

class Objects
dog The dog door
Cellular phone Your iPhone 11
people Ma
Drink zero COLA

Of course, when Coke Zero represents a beverage, which can be used as an object drink; and when Coke Zero also represents a beverage when it? Then it is also a class, then the relationship with the beverage becomes a parent class and subclass inheritance of.

Three, java in the class:

1. The class definition:

Keywords: class
format:

public class 类名
{
	(属性定义)  ......
	(方法定义)  ......
}

Property defines the class 2:

public 属性类型 属性名;

3. The method of class definitions:

(JAVA Method equivalent function in C)

public 方法返回值类型 方法名(参数类型 参数名)
{
	......
	(方法的代码)
	......
}

Note:
1, the method requires the use of his property, then write directly attribute name.
2, parameters of the method is a method required external conditions.

4. The method of constructing the class definition:

public 类名(参数类型 参数名,……)
{
	......
	(方法的代码)
	......
}

Role: to create an object when the object is initialized to the property assignment.

Four, java objects in

1. Creating an object

Keywords: new
format:

类名 对象名 = new 构造方法(参数值, ......);

2. The method of use of the object

对象名.方法名(参数值, ......);

Fifth, the class inheritance

If you already have a class is "student", its properties are: school, grade, name, student number; this time if you want to build a class is "College," which attributes are: school, grade, specialty, name, student number. At this time, we can use the method inherited, so that "students" inherit "student", inheritance allows a sub-class has properties and methods of the parent class , and you can add more new properties and methods can also be rewritten from the parent class inherited methods.

1. inherited format

Keywords: extend
the format:

public class 子类名 extends 父类名
{
	......
	(属性的定义) ......
	(新属性的定义) ......
	(方法重写) ......
	(新方法定义) ......
	......
}

2. Access

The modified control the extent to which content can be used.

The current class Under the same package Subclasses in different packages other
public
protected
default
private

3. Rewrite

Why rewrite?
If a parent class does not satisfy the needs of the subclass, the subclass can cover the method of the parent class.
Requirements:
1, there must be inheritance.
2, seen in the parent class subclasses.
3, declarations of methods, access rights can be expanded, the return type of the method, the method name, parameters must be exactly the same.

4. Automatic Transformation

What is the automatic transition?
Subclass object automatically becomes a parent class object.
After automatic transition, the contents of whichever method is called a subclass.

Published 10 original articles · won praise 14 · views 652

Guess you like

Origin blog.csdn.net/weixin_42368748/article/details/100746124