JavaSE-07 class

Source: Internet
Author: User
Tags instance method modifiers

Key points of Xi
    • Process oriented
    • Object oriented
    • Abstract
    • Class
    • How to construct a class
    • Common keywords in a class
    • Member methods of the class
    • member variables of the class

Process-oriented procedures

Concept of the program

The term "program" comes from life, and usually refers to an established way and process of accomplishing something.

You can consider a program as a description of the execution of a series of actions.

Computer programs

A collection of ordered instructions written to perform certain operations or to resolve a problem.

Definition of process-oriented procedures

program = data + algorithm + documentation;

Variable
    • A pointer to an object or an identifier that contains a value can be called a variable.
    • When the type of a variable is a basic type (short, byte, int, long, float, double, Boolean, char), the content of the variable is a value, and when the variable is of type object and its subclass, the content of the variable is a reference to an object.
Process Control

Common process controls: order, branch selection, loop.

Object-oriented What is an object?
    • All things are objects! objects have attributes (static attributes) and behaviors (dynamic properties).
    • Object objects are also known as instance--instance.
What is object-oriented?

The world of program is virtual, object-oriented technology uses "object-oriented thought" to describe "object-oriented world", and realizes the consistency between virtual world and real world. More in line with people's thinking habits.

The benefits of object-oriented design (OOD) and Development Program (OOP)

    • Easier and smoother communication between users, software designers, software developers
    • Improve design and development efficiency

Abstract in the software design phase, how to use object-oriented program design?

Question: How do you describe them in the computer world?

    • The Java language is an object-oriented language, do you want a small animal to describe the above object? The virtual world and the real world one-to-one mapping, too cumbersome, not smart. So the object-oriented programming language uses the class template, where programmers use class to describe them when they write code: classify these animals, find commonalities, make templates, and use templates to generate a concrete object when the computer virtual world needs specific objects.
    • The object-oriented programming is to abstract objects out of class.

Abstract class Three steps from the real world

    • First step: Discovering classes

    • Step two: Discover the properties of the class

    • Step three: Discover the methods of the class

    • Fourth step: Using Class diagrams to describe classes

On-Machine exercise: Developing an electronic pet system using code
    1. There are dogs and penguins in the system.
    2. The system offers pet selection//Pending completion
    3. Dogs have a variety of options, penguin Sex Optional//To be completed

Class Java class template

[modifier] class name {

0 to multiple construction methods (also known as constructors, constructors)

0 to multiple member variables (also known as properties, fields, domains). Attribute specifically with Getter and setter accessors)

0 to multiple member methods

}

Access modifiers for class: None, public, final, abstract

Note: Classes that do not have modifiers are only visible in this package.

Use of the class
Instantiate class dog  dog;dog  =  new Dog ();//access the members of the class Dog.nickname= "Wang Wang";//Assign a value dog.print () to a member variable;//Call a member method in a class

Syntax analysis of constructing method of class construction method

[modifier] Constructor Method name (formal parameter list) {

Executable statement;

}

Modifiers: public, protected, private

Method Name: Class name

Private is often used to design singleton patterns, such as:

public class Dog {     private static dog Currentdog = new Dog ();        public static Dog Getdog () {         return currentdog;     }        Don ' t let anyone else instantiate this class     private Dog () {     

  

The constructor method is used to construct an instance of the class. Java uses the new keyword to invoke the constructor method, which returns an instance of the class.

For example: Dog dog = new Dog ();

The fundamental way for a class to create an object is to construct an instance by constructing a method.

If you do not have a custom constructor method, the system provides a non-parametric construction method by default. If you customize the construction method, the system no longer provides an argument-less construction method.

Method of constructing with parameters

If you want to complete the initialization of an instance while constructing an instance of a class, you can define a construction method with parameters.

The formal parameter name of the constructor is repeated with the name of the member variable, and the local variable overrides the member variable according to the Java rule: local variable name and member variable name. If you need to access member variables, you need to add the This keyword before the member variable name.

Constructing method overloads
    • Overloaded Concepts: Method names are the same, parameter items are different. Is independent of the return value of the method, the modifier.
    • Construction methods can be overloaded, and normal methods can be overloaded.
    • The invocation of the overloaded constructor method, which corresponds to the formal parameter and the argument.
Mutual invocation of construction methods

Common keywords in class this keyword usage
    • The This keyword represents a reference to the current object, and is used to differentiate member variables of the same name.
    • References the object that the constructor is initializing in the construction method.
    • The object that invokes the method is referenced in the normal method.
member variables and member methods for static adornments
    • Static decorated member variables and member methods can be accessed through the class name. Member.
    • Static can be used to decorate blocks of code.
    • Static modified member variables, member methods, code blocks, and corresponding data and code are persisted in the memory of the data area.
    • The static decorated member variables, methods, inner classes, initialization code blocks are all classes, and none of the static adornments belong to the instance.

Example code:

Class Web {Public final static string logo= "Sina net";//class variable public static int count = 0;//class Variable public string IP = Nu        ll;//instance variable public Web (String IP) {count++;    this.count++;//prompts the static way to access This.ip = IP;        }/** class method: Statistics Website Visitor number */public static void PrintCount () {System.out.println ("Welcome" +logo);           System.out.println ("Current visitor:" + count);          /** instance method: Outputs the current remote User IP address */public void Printip () {System.out.println ("The current login IP is:" + IP);    System.out.println ("-------------------------\ n"); }}/** Test class */public class Staticdemo {public static void main (string[] args) {Web local1 = new Web ("10.12.1.3"        );        Web.printcount ();        Local1.printip ();        Web Local2 = new Web ("192.168.1.2");        Web.printcount ();        Local2.printip ();        Web Local3 = new Web ("20.38.4.5");        Web.printcount ();        Local3.printip ();        Web Local4 = new Web ("121.124.13.32");    Web.printcount ();    Local4.printip ();        Web LOCAL5 = new Web ("168.77.88.44");        Web.printcount ();    Local5.printip (); }}

  

Final-Modified member variables
    • A member variable that is decorated with a final is called a constant.
    • Static final variable type variable name: constant.

Member method syntax format for a class

[Modifier] method returns a value type method name (parameter list) {

Method execution statement;

}

Modifier

Public, protected, private, static, final, abstract.

Where public, protected, private can only choose one, abstract and final can only appear first; they can all be combined with static to modify the method.

return value

The return value needs to return the result using the return statement, and the type of the result must match the return value type. If there is no return value, the method's return value type must be void.

public class Test {public    int: age;     Public Test Grow () {       age++;       Return this;//returns the current object   } public   static void Main (string[] args) {       Test t=new test ();       T.grow (). Grow (). Grow (). grow ();       System.out.println ("age=" +t.age);}   

  

Invocation of a method in a class
    • A class method can only invoke another class method, syntax format: Class name. Method Name. The class name can be omitted.
    • Invocation between instance methods, syntax format: Object name. Method Name. Actually write this. Method name. This can be omitted.
    • An instance method can call a class method (a static decorated method).
    • The mechanism by which a method passes a parameter (how does an argument pass a parameter to a formal parameter?) )。
    • A method in a Java class passes a parameter by value, that is, a copy of the argument is passed to the parameter.
    • The base type passes the value, and the reference type passes the address of the object (not a copy of the object).
    • String type data that, if it is defined as a double-quote type of data, passes the data by value, passing a reference to the string object if the string is constructed as a construct.
A method for changing the number of formal parameters

When you define a method, if you add three points (...) after the type of the last formal parameter, it indicates that the parameter can accept more than one argument value, and that more than one parameter value is passed in as an array.

Public  static  void  Targs (int  A, string ... names) {for        (string  item:names) {       SYSTEM.OUT.PRINTLN (item);}}

Note: A variable number parameter can only have one method and is placed at the end of the method parameter list.

Member variable syntax format for a class

[modifier] Type member variable name [= default value];

Modifier

Public, protected, private, static, final

Where public, protected, private can only choose one, can be combined with static, final decoration member variables.

member variables and local variables

Member variables: instance variables (variables not modified with static), class variables (static decoration)

Local variables: Formal parameters, method local variables, local variables within code blocks

Try to avoid accessing class variables through instances in real development.

On-Machine exercise: Developing an electronic pet system using code
    1. The system has dogs and penguins//completed
    2. System offers pet selection
    3. Dogs have a variety of options, penguin sex is optional

JavaSE-07 class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.