Java4android (object-oriented basis)

Source: Internet
Author: User

One

1. What is object-oriented?

Object-oriented is a programming method; object-oriented is a way of thinking; not a programming language.

2. How should we study object-oriented?

Mastering an object-oriented language syntax;

Grasp the object-oriented thinking mode;

Familiar with object-oriented design principles;

Mastering object-oriented design patterns

3, what is the object-oriented thinking method

First determine who will do, and then determine how to do;

First of all, consider the whole, followed by partial;

First, consider the abstraction, then consider the specific

Two

1. Methods for creating classes

Class name

{

Attribute (also called member variable, mainly used to describe the state of a class)

method; (also called a member method, used primarily to describe the behavior of a class)

}

2. Methods for creating objects

Format: Class Name Object name = new class name ();

For example: Dog dog = new Dog ();

Dog dog = Create a reference to the dog and put it in the stack memory

= new Dog (); Create a dog object, put it in heap memory

      

3. References to Objects and objects

object is placed in heap memory, and the object's reference is placed in the stack memory

Three

How objects are used

Use objects to invoke variables and functions:

Object. Variables

Object. function ()

Ways to create multiple objects

Dog D1 = new Dog ();

Dog D2 = new Dog ();

How to create and use anonymous objects

You can not define a reference name for an object. The method that calls this object directly. Such objects are called anonymous objects, such as:

New Dog (). Jump ();

New Dog (). Jump ();

When it's gone, the second anonymous object is the newly created one.

Four

Overloading of functions

 

Judging criteria: two or more functions are in the same class; function names are the same; the function has a different argument list

    

class a{    void  Funa () {        System.out.print ("Funa function without arguments" + "\ n");    }         void Funa (int  a) {        System.out.print ("Funa function with an integer parameter");}    }

Two Funa functions are overloaded

class texta{    publicstaticvoid  main (String args[]) {        new A ();                A.funa ();        A.funa (ten);    }}

So the Funa function can be called

function of the constructor

The constructor is called after new.

Grammar:

The constructor function name must be the same as the class name;

The constructor does not have a definition of the return value;

Use new to call the constructor;

If there is no constructor in the class, the compiler automatically adds a null argument to the constructor with an empty method body. If there is, it will not be added.

class a{    A () {          // constructor     }        }


Example:

class person{Person    () {    }        person (String N,int a) {// own defined constructor overloaded        name = N;         = A;    }        String name;     int Age ;}
class textperson{    publicstaticvoid  main (String args[]) {        new person ("Zhangsan", "ten");         New Person ("Lisi");                System.out.print ("Person1 's name is:" +person1.name+ ", Age is:" +person1.age+ "\ n");        System.out.print ("Person2 's name is:" +person2.name+ ", Age is:" +person2.age);}    }

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.