This article will help you understand the use of classes and objects
Object: An entity used to describe objective things, consisting of a set of properties and methods.
Attributes: Are the characteristics of the object, such as height, weight, color
Methods: The behavior of an object, such as running and jumping
Classes: Classes are templates that define the properties and methods that an object will have. Classes are abstract concepts such as “books”
Define the class
Grammar:
public classThe name of the class{properties1Type attribute of1; The type attribute n of attribute n; ... methods1; Methods m; ... }Copy the code
To define a class, you can follow three steps: 1. Define the name of the class; 2. Write the methods of the class
Tap: Student class
public class Student{
String name;// Name attribute
int age;/ / age
public void sayHi(a){
System.out.println("Hello, I am:"+name+",我 "+age+"The"); }}Copy the code
Using the object
Grammar:
Class name Object name =newThe name of the class ();Copy the code
Click: Create student object
public class Student{
String name;// Name attribute
int age;/ / age
public void sayHi(a){
System.out.println("Hello, I am:"+name+",我 "+age+"The");
}
public static void main(String[] args) {
Student student=new Student();
student.name="White";
student.age=20; student.sayHi(); }}Copy the code
Reference class property: object name. Property refers to the method of the class: the object name. The method name ()
OOP
Object Oriented Programming (OOP) is a kind of Programming architecture rather than face to face Programming with objects.
Why object oriented:
Object orientation is consistent with human thinking habits and can better use computers to simulate the real world.
Object orientation improves the reusability of programs, programs can be built like building blocks, reduce code, improve the speed of development.
Information hiding improves program maintainability and security, reducing the risk of large-scale software development
Java is object-oriented language, object-oriented development is the trend of development.
Documentation comments
Earlier we learned that there are two types of comments, single-line comments // and /** / multi-line comments. There is also a comment for generating help documents, document comments /** */. Type /** enter in Eclipse and the rest will be generated automatically.
Presentation:
import java.util.Arrays;
/** * Test class *@author Jackie
*
*/
public class Test {
/** * quantity */
int num;
/** * program entry *@param args
*/
public static void main(String[] args) {
String[] names= {"Zhang"."Bill"}; System.out.println(Arrays.toString(names)); }}Copy the code
Some common Javadoc tags are listed below
The label | describe | The sample |
---|---|---|
@author | Identifies the author of a class | @author Jack |
@deprecated | Specifies an expired class or member | @deprecated description |
@param | Specifies the parameters of a method | @param parameter-name explanation |
@return | Indicates the return value type | @return explanation |
@since | Flag when a particular change is introduced | @since release |
The finished code contains comments, and you can use the command to generate the help document javadoc -d apidoc *.java
Search the concerned public number “enjoy wisdom peer”, the first time to obtain technical dry goods