Java Object-oriented notation (1)

Source: Internet
Author: User

1. Create a class person


To create a memory partition diagram of an object:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/12/wKiom1VytnKikTlZAAD6vbVLNb8485.jpg "title=" Qq20150606165859.jpg "alt=" Wkiom1vytnkiktlzaad6vbvlnb8485.jpg "/>

Assigning values to attributes, acting as

Object. property = Assignment.

Class person{string name;int age;public void Tell () {System.out.println ("name:" + name+ "+" Age: "+age);}} public class MethodDemo01 {public static void main (string[] args) {person per = Null;per = new Person ();p er.name = "Thystar "; Assignment per.age = 22;per.tell ();}}


After assignment, add data to the heap memory space

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/12/wKiom1VyuArhLG91AAEE-kBGbe8001.jpg "title=" Qq20150606170549.jpg "alt=" Wkiom1vyuarhlg91aaee-kbgbe8001.jpg "/>


The explanation that I see at the Geek College is that it's good to write it down first: Http://www.jikexueyuan.com/course/111_2.html?ss=1


2. Object-Oriented Programming

Encapsulation of the class:

To avoid arbitrary changes to properties in the class, define the property as private, called with Getter and setter methods: Right--->source-->Generate Getters and Setters

Class Person{private string name;private int age;public string getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} public void Tell () {System.out.println ("name:" + getName () + "" + "Age:" +getage ());}} public class MethodDemo01 {public static void main (string[] args) {person per = Null;per = new Person ();p er.setname ("Thysta R ");p Er.setage;p Er.tell ();}}


Geek College Change course website: http://www.jikexueyuan.com/course/113.html



Anonymous objects

Class student{public void Tell () {System.out.println ("Hello thystar!");}}  public class Demo01 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stubstudent   Stu = new Student (); Stu.tell ();//Anonymous Object New Student (). tell (); Can only be used once}}


Construction method

Class Student{public Student () {System.out.println ("Hello Thystar");}}  public class Demo01 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stubstudent Stu = new Student ();}}

The construction method name is the same as the class name, and the tangent has no return value, but the parameter can be passed.

Class student{string Name;int age;public Student (String name, int age) {System.out.println ("name:" + name+ "" + "Age:" +age) ;}}  public class Demo01 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stubstudent Stu = new Student ("Thystar", 22);}}

The construction method is automatically defined by the system and its methods can be overloaded.


Geek College Change course website:Http://www.jikexueyuan.com/course/113_3.html?ss=1



Reference delivery

Class Ref1{int temp = 10;}  public class Demo02 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stubRef1 R1 = new Ref1 (); r1.temp=20; System.out.println (r1.temp); tell (R1); System.out.println (r1.temp);} public static void-Tell (Ref1 r2) {r2.temp = 30;}}

Output Result:

10

20

30

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/10/wKioL1VyxR7Aa3eTAAEmCNpedZY175.jpg "title=" Qq20150606175453.jpg "alt=" Wkiol1vyxr7aa3etaaemcnpedzy175.jpg "/>


public class Demo3 {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stubstring s TR1 = "Hello"; System.out.println (STR1); tell (STR1); System.out.println (STR1);} public static void-tell (String str) {str = "Thystar";}}

Output Result:

Hello

Hello


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/10/wKioL1VyxtOAovuHAAFS7whIa9Y783.jpg "title=" Qq20150606180125.jpg "alt=" Wkiol1vyxtoaovuhaafs7whia9y783.jpg "/>


Geek College Course website: http://www.jikexueyuan.com/course/114.html



this keyword

How to use

    • Represents a property in a class and calls a method

    • Represents a construction method in this class

    • Represents the current object


Class person{private string name;private int age;public person (String n,  int a) {        this ();  //calls the constructor method in this class this.name =  n;//represents the attribute This.age = a;} Public person () {System.out.println ("xxxxxxxxxxxxxx");} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;} Public void tell () {System.out.println ("Name:" + this.getname () + " " + "Age:" +this.getage ());// Calling methods}}public class thisdemo01 {/** *  @param  args */public static  Void main (String[] args)  {// todo auto-generated method stubperson p  = new person ("Thystar",  22);p. Tell ();}}


Static keyword

1. Use the static Declaration property--"Static Declaration global property

2. Using the static declaration method--called directly through the class name

3. Note: When using the static method, you can access only the properties and methods of the static declaration, not the properties and methods of the static declaration.


Static properties and methods are called directly with the class name.


Geek Academy Address: http://www.jikexueyuan.com/course/114_3.html?ss=2















Java Object-oriented notation (1)

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.