package Demo01; /** * Object-oriented: * Procedure-oriented and object-oriented: * Process-oriented: Process-oriented is to analyze the steps required to solve the problem, and follow these steps to realize step by step * Object-oriented: Object-oriented is to decompose the things that make up the program into various objects (find participants), and the purpose of creating objects is not to complete a step, * but to describe how something behaves throughout the problem-solving steps * * Object: An entity that describes objective things, consisting of a set of properties and methods * class: an abstraction of an object (extracted part of the image) class * First there are specific objects, then abstract the like between each object, and summarize the categories to recognize other objects through categories * * Attributes in the object: -- "The specific characteristics of the object (basic information) noun * Each property of each object has a specific value; * For example: The name, age, weight... * Defined in the class, called member variables/class variables/member properties, which act on the entire class body * Format: * [modifier] attribute type attribute name = [default]; optional * [public] String name = [null] ; * * Method in the object: --> The operation (behavior) performed by the object Verb * Definition format: * [modifier] method's return value type method name(){ * java statement * } *Parameter passing of methods in java: passing by value both primitive data types (copy of value) and reference data types (value reference) * * class is an object type * * Template: write reality into the program * So java programs are organized in classes * The keyword class defines a custom data type * * Write a class template: A class is a template, an abstract concept that specifies the properties and methods that an object will have *[1] Write a type template using the class keyword *[2] Write the properties of the class *[3] Method of writing class * * Use template: * [1] Create an instance of a class through a class * [2] Use objects to assign values to properties * [3] Use the object to call the method * * * Method overloading: * is a way for classes to handle different types of data in a uniform way * * Multiple functions/methods with the same name: exist at the same time, with different parameters/number/type (same name and different parameters) * */ public class Person { //[1] Write the type //[2] Write the properties of the class String name; //declare a member variable name, the default value is null int age; //declare a member variable age, the default value is 0 char gender; //declare a member variable gender, the default value is blank //[3] Method of writing class public void info(){ System.out.println("My name is: "+name+", this year: "+age+", it is a "+gender+" sex."); } // no-argument constructor public Person(){ //When there is no parameterized structure written, the system will default to a parameterless structure. Once there are parameters written by hand, the system will no longer default. } // constructor with parameters public Person(String name,int age,char gender){ this.name = name; this.age = age; this.gender = gender; //this: represents the current object, you can assign properties in the method to member variables (member properties) } // main method public static void main(String[] args) { //[1] Create object --" class name object name = new class name (); Person p1 = new Person(); Person p2 = new Person("Flowers cross the horizon", 18, 'male'); //[2] Assign a value to the attribute --" object name. attribute = value; p1.name = "Mo Xiaohan"; p1.age = -12; p1.gender ='man'; //[3]Using method --" object name.method name(); p1.info(); p2.info(); } }
Object-oriented knowledge essentials
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324402594&siteId=291194637
Recommended
Ranking