The method of construction and method overload

In the last chapter Bowen "Java foundation to explain," we understand the advantages of JAVA programming language, basic output method, and the concept of the eight basic types and classes. So I bit the students in this blog post in JAVA language introduced two very common means of two - constructors and method overloading.

First of all, I have to explain the method : JAVA language method, equivalent to our function in the C language.
(Before we learn JAVA few lessons, I would like to make it clear: we have a lot to learn though C language and JAVA language is the same, but we want to come over as soon as possible to correct the terminology, it can no longer be used in the C language habits only continue to learn from excellent knowledge, slightly worse than previously thought and get rid of, we can progress)

After learning "method", we are now into the theme.

Construction method:

What is a "constructor" mean?
Here, we introduce by an example for the students.
Or working on lesson screen coordinates, we have the first class code out:

SimplePoint.java:

class SimplePoint {
    int row;
    int col;

    void showPoint() {
        System.out.println("(" + row + "," + col + ")");
    }
}

DemoSimplePoint.java:

class SimplePointDemo {
    public static void main(String[] arg) {
        SimplePoint b = new SimplePoint();  
        b.row = 10;
        b.col = 20;
        b.showPoint();
    }
}

I believe the lesson learned students do not have the code here have any doubts.
So I put in a request to:
requirements when we define an instance of a class, it is necessary that all members of the assignment.
This requirement for students to learn the current course, it is almost impossible, so, I now introduce a method to solve this problem - Constructor

Let me introduce rules constructor:

Rule constructor
1 . Constructor must named classes;
2 . Constructor not declare a return value (void nor);
3 . The user can not call the constructor;
4 . Constructor is called when the new new;
. 5 . Constructor the main purpose is to initialize the value of each member (if a class constructor If you do not write, then it is the default constructor as follows: initial value for all members of this way: 0 (byte, char, short , int, long type)
0.0 (float, double type) false (boolean type) null (class type, class type to be understood that where the variable is "pointer"))

Well, we now look at the code used to implement the above requirements:
SimplePoint.java:

public class  SimplePoint{
    private int row;    //private表示这个变量只能在该类中被调用。用于防止外类修改该成员
    private int col;

    public MecPoint(int x, int y) { //这就是我们所谓的“构造方法”
        setRow(x);
        setCol(y);
    }

    public void setRow(int x) {
        if(x <= 1 || x > 25) {  //对x范围进行约束
            x = 12;
        }
        row = x;
    } 
    
    public void setCol(int y) {
        if(y <= 1 || y > 80) {  //对y范围进行约束
            y = 40;
        }
        col = y;
    }

    public void printPoint() {
        System.out.println("(" + row + "," + col +")");
    }
}

Well, that's the constructor of the examples and rules.
Now we enter the second issue - Method overloading it!

Method overloading:

Method overloading, this knowledge, really made me understand a little to some "object-oriented" feeling, because it lets you repeat the definition of a method for different method overloading, there are different approaches, which means , we require users to input content is no longer single, but almost can meet a variety of legal entry.
Having said that, I now introduce under the "method overload" rule it:

Method overloading rules :
the same name (1) method;
(2) different number of parameters and parameter types;
(3) the type of return value of the difference is not the decisive factor; ((1) and (2) is a decisive factor)
(4) as long as (1) and (2), it may be overloaded constructor.

Well, or take the above example, we are asking the user to enter a parameter or two parameters, no parameters or even an object parameter (variable type is a class) may be, then the relevant code is as follows:

SimplePoint.java:

public class  SimplePoint{
    private int row;    //private表示这个变量只能在该类中被调用。用于防止外类修改该成员
    private int col;

    public SimplePoint() {              //无参构造方法
        System.out.println("This is none argument constructor!");
        setRow(0);
        setCol(0);
    }

    public SimplePoint(int x, int y) {  //2个参数的构造方法
        System.out.println("This is 2 argument constructor!");  
        setRow(x);
        setCol(y);
    }

    public SimplePoint(int x) {         //一个参数的构造方法
        System.out.println("This is signle argument constructor!"); 
        setRow(x);
        setCol(0);
    }

    public SimplePoint(SimplePoint point) { //对象作为参数的构造方法
        System.out.println("This is object argument constructor!"); 
        setRow(point.row);
        setCol(point.col);
    }

    public void setPoint(int x, int y) {
        setRow(x);
        setCol(y);
    }

    public void setPoint(int x) {
        setPoint(x, 0);
    }

    public void setPoint(SimplePoint source) {
        setPoint(source.row, source.col);
    }

    public void setRow(int x) {
        if(x <= 1 || x > 25) {  //对x范围进行约束
            x = 12;
        }
        row = x;
    } 
    
    public void setCol(int y) {
        if(y <= 1 || y > 80) {  //对y范围进行约束
            y = 40;
        }
        col = y;
    }

    public void printPoint() {
        System.out.println("(" + row + "," + col +")");
    }
}

As I mentioned in my "data structures and algorithms" column before, we write the code to be logical, method (function) features a single, complete and total functionality, so we now look at the code to optimize the above-mentioned two documents :
MecPoint.java:
(because of my micro-techniques are code-leader purse, so that the file named I MecPoint.java)

public class  MecPoint{
    private int row;    //private表示这个变量只能在该类中被调用。用于防止外类修改该成员
    private int col;

    public final int MIN_ROW = 1;
    public final int MAX_ROW = 25;
    public final int DEFAULT_ROW = 12;
    public final int MIN_COL = 1;
    public final int MAX_COL = 80;      //屏幕点坐标范围:共25行、80列
    public final int DEFAULT_COL = 40;  //默认屏幕点坐标错误时,设为中值   

    public MecPoint() {
        System.out.println("This is none argument constructor!");
        setRow(0);
        setCol(0);
    }

    public MecPoint(int x, int y) {
        System.out.println("This is 2 argument constructor!");
        setRow(x);
        setCol(y);
    }

    public MecPoint(int x) {    
        System.out.println("This is signle argument constructor!"); 
        setRow(x);
        setCol(0);
    }

    public MecPoint(MecPoint point) {
        System.out.println("This is object argument constructor!"); 
        setRow(point.row);
        setCol(point.col);
    }
    
    public void setPoint(int x, int y) {
        setRow(x);
        setCol(y);
    }

    public void setPoint(int x) {
        setPoint(x, 0);
    }

    public void setPoint(MecPoint source) {
        setPoint(source.row, source.col);
    }

    public void setRow(int x) {
        if(x <= MIN_ROW || x > MAX_ROW) {   //对x范围进行约束
            x = DEFAULT_ROW;
        }
        row = x;
    } 

    public int getRow() {
        return row;
    }
    
    public void setCol(int y) {
        if(y <= MIN_COL || y > MAX_COL) {   //对y范围进行约束
            y = DEFAULT_COL;
        }
        col = y;
    }

    public int getCol() {
        return col;
    }

    public void printPoint() {
        System.out.println("(" + row + "," + col +")");
    }
}

You can see, I added three new functions: reading abscissa, ordinate reading, moving coordinate function,
and write a lot of type "public final int" variables for type here can be understood as int type "macro" (the knowledge that I will explain in detail in future blog post), so our code logic is very clear!

Well, now, two specific issues have been explained completed, the next to show himself, specifically how to call these methods now:

DemoMecPoint.java:

public class  DemoMecPoint{
    public static void main(String[] args) {
        MecPoint pointFirst = new MecPoint();
        System.out.println("演示 无参 构造结果:");
        pointFirst.printPoint();

        MecPoint pointSecond = new MecPoint(5, -7);
        System.out.println("演示 双参 构造结果:");
        pointSecond.printPoint();

        MecPoint pointThird = new MecPoint(20);
        System.out.println("演示 单参 构造结果:");
        pointThird.printPoint();

        MecPoint pointForth = new MecPoint(pointFirst);
        System.out.println("演示 对象 构造结果:");
        pointForth.printPoint();
    }
}

Well, now we have to show operating results under the code:
Here Insert Picture Description

So, all the content so far, this post will explain complete.
JAVA language, with respect to the new to the students, it may also be suited to some, however, with in-depth study, I'm sure, learned how to JAVA, the students will be very inconsistent in C, because JAVA is too free, too convenient!

If the film on Bowen's fianl keywords and students we have been questionable in the use of the static keyword, check out my blog - "Detailed final and static."

Guess you like

Origin www.cnblogs.com/codderYouzg/p/12416426.html