Fourth day of Java learning

Source: Internet
Author: User

Today May 3, 2016, Java study fourth day!

P76 3.4 Construction Method

    • The name of the construction method is consistent with the class name;
    • The constructor method is not allowed to be declared by the return value type; (no return statement)
    • There is at least one construction method in a class.

By default, there is an argument-free construction method.

Class person{//classes name Capitalize first letter

Public person () {//no parameter return value method

}

}

First paragraph of code:

Package COM.CQVIE.YYC;
If you do not have a write constructor method, the system creates an parameterless constructor by default, such as:
Public person ()
//{
//
//}
If the construction method is written, the system no longer creates a default constructor method, for example, it has written a constructor method:
Public person (String n,int a)
//{
//...
//}
The system will no longer create an parameterless construction method by default
Person per =new person ();//This sentence compiles an error
public class Person {
public String name;
public int age;
Public person () {
Name= "Anonymous";
age=1;
}
Public person (String n,int a)//function overload
{
SetName (n);
Setage (a);
}
public void Tell () {
SYSTEM.OUT.PRINTLN ("I Call" +name+ ", this year" +age+ ");
}
public void SetName (String N) {
Name=n;
}
public void setage (int m) {
if (m>=0&&m<=250) {
Age=m;
}
}
Public String GetName () {
return name;
}
public int getage () {
return age;
}

}

The second piece of code:

Package COM.CQVIE.YYC;

public class Persontest {

public static void Main (string[] args) {
Person P=new person ();
P.tell ();
Person P1=new person ("bloomer tooth", 80);
P1.tell ();
}

}

Output Result:

My name is John Doe, 1 years old this year
My name Bloomer, 80 years old this year.

P125 this keyword

    1. "This. Attribute" denotes this class of properties

Code:

public class person{

private String name;

private int age;

Public person (String Name,int age) {

This.name=name;

This.age=age;

}//setter, getter slightly

Public String Getlnfo () {

Return "Name:" +name+ ", age" +age;

}

}

public class Testdemo {

public static void Main (String args[]) {

Person per = new Person ("Zhang San", 20);

System.out.println (Per.getlnfo ());

}

}

Program Run Result:

Name: Zhang San, age: 20

In all future developments, in order to avoid unnecessary bugs, this book recommends that you use the "this. Property" method whenever you are invoking a property in a class.

2. (this) call the method p128-129

All construction methods are called by default when the object is instantiated, and are called before the normal party is called, so the operation using "this ()" To call the construction method must be placed on the first line of the constructor method.

3.this indicates the current object: P132 not yet.

P80 3.5 Anonymous Object-------------------

P81 3.6 Simple Java class------------------

P84-103 3.7 Array

Declare and open an array (also known as an instantiated array)

Data type array name []=new data type [length]; int a[] =new int[3];

data type [] array name =new data type [length]; int [] a=new int [3];

Fourth day of Java learning

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.