Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the core of Java object-oriented and class

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the core of Java object-oriented and class". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the core of Java object-oriented and class".

What is the object?

Everything that exists objectively is an object.

The composition of an object

Attribute, behavior

What is the class?

A class is an abstraction of the commonness of a large number of objects

Class is the subjective reflection of objective things in the human brain.

A class is a template for creating objects

Composition of the class

Attribute, behavior

To put it simply, a class is to write public features together, and then all objects created by this class will have this property. For example, people can eat and talk. We are objects, and we belong to the category of people, so we also eat and talk.

Definition of classes in Java

Syntax:

Public class Student {/ / write member attribute and member method} / / Student is the name of this class, which can be customized.

Declaration of member variables:

Public class Student {String name; / / define the attribute of a name int age; / / define the attribute of an age / / you can also directly assign the initial value int age=10;}

Declaration of member methods:

Public class Student {String name; / / defines the property int age of a name / / define an age attribute public void show () {/ / this is the member method System.out.println ("my name is:" + name+ "this year:" + age+ "year") / / when an object is created by this class, use this method to output this sentence}}

Create an object from the class:

Syntax: variable name = new class type name ()

Student stu = new Student (); / / means that stu is an object of the class Student and has properties and methods of the class

Call the member method of the object:

Syntax: object name. Method name ([parameter, …])

Student stu = new Student (); stu.show (); / / use this object to call the show method of the class. / / because this method does not need to pass parameters, it can be called directly.

Method overload:

If there are multiple methods with the same name but different parameters in a class, it is called method overloading. If you only need to perform one operation, having the same method name increases the readability of the program.

/ / two integers add int add (int num1,int num2) {return num1 + num2;// two double floating point numbers add int add (double num1,double num2) {return num1 + num2;// two float floating point numbers add int add (float num1,float num2) {return num1 + num2 / / the names of the three methods are systematic, but the parameters that need to be passed in are different, so the overloading of the method / / having the same method name will increase the readability of the program.

The use of the construction method:

The Java language builds an object through the constructor of a class, which is similar to the declaration of a normal method, but has some characteristics: the name of the constructor must be the same as the class name.

Function:

Generally used to initialize member properties and methods, that is, after the new object is generated, the properties and methods of the object are called. The constructor runs as soon as the object is established, initializes the object, including properties, and executes the statements in the method. The general function is executed only when the object is called, and the function is added to the object in the way of ". Method name.

Public class Student {/ / No-parameter constructor public Student () {System.out.println ("this is a constructor")} / / when an object is new, this method will run automatically And execute the statement} / / parameterized constructor public Student (String name) {this.name = name } / / when you new an object, you need to pass a String value after the new object. / / the constructor assigns this value to the name / / example of the global variable in the class: Student stu = new Student ("Xiao Hong"); / / this is the initialization of the name value when the new object is used.

The usage scenario of the constructor:

In addition to instantiating objects, constructors can assign values to member variables

This keyword:

This is a special reference to the current object

If there is a conflict between the naming of local variables and member variables, you can use this. The way member variable names distinguish between instance variables and local variables

A constructor needs to call another constructor of the same class, which can be called by this (), but this () must be written on the first line

Use:

/ / take this code block as an example / / when a name value is passed in, we need to pass this value into the * * global variable * * to use / / if this is not added, name will match the name in this method, because it belongs to a local variable in the method / /, so we add the this keyword before receiving the variable Refers to the global variable public Student (String name) {this.name = name } Thank you for reading, the above is the content of "what is the core of Java object-oriented and classes". After the study of this article, I believe you have a deeper understanding of what is the core of Java object-oriented and classes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report