[Zero foundation? Fast Java development in a day] Day2 - First knowledge of object-oriented

Table of contents

foreword

1. Use of variable parameters

2. Constructor

3. Package

1. Package creation

2. Use of packages

3. Package naming convention

4. Commonly used packages

5. Access modifiers

6. Inheritance 

7. The super keyword

8. Method rewriting Override

Write at the end:


foreword

My basic learning of java was followed by Han Shunping's java course~

This article covers the different knowledge points between java and C++ in episodes 216 ~ 305 of the video~ 

1. Use of variable parameters

Java supports variable parameters. When you need to pass some values, and these values ​​are of the same type,

Variable parameters can be used to pass, see an example:

public class VarMethod {
	public static void main (String[] args) {
		HspMethod m = new HspMethod();
		System.out.print(m.Sum(1, 5, 10));
	}
}

class HspMethod {
	// 我们可以把 nums 看成是一个数组
	int Sum(int... nums) {
		int sum = 0;
		for(int i = 0; i < nums.length; i++) {
			sum += nums[i];
		}
		return sum;
	}
}

We can treat the parameter nums that receives variable parameters as an array,

In this way, you can calculate the addition of any integer value at will.

output:

You can see that the output is fine.

Here are some details to pay attention to,

1. Variable parameters can be 0 or more;

2. If it is with ordinary parameters, variable parameters must be placed at the end;

3. Only one variable parameter can appear in a formal parameter list.

2. Constructor

The constructor of java is almost exactly the same as the constructor of C++.

Let's take a look at the syntax of the constructor:

public class Construct {
	public static void main(String[] args) {
		Person p = new Person("张三", 18);
		System.out.print(p._name + (p._age + ""));
	}
}

class Person {
	String _name;
	int _age;

	public Person(String name, int age) 
	{
		_name = name;
		_age = age;
	}
}

output:

In other respects, it is basically the same as C++.

3. Package

1. Package creation

The java package is distinguished according to the directory,

To build a package in IDEA, use '.' as the separator of the first-level directory:

For example, in the src directory, we have built two packages, one is com.a and the other is com.b. This is the effect:

This way we can create two classes with the same name without conflicting:

2. Use of packages

Let's write a piece of code to call these two packages:

package com.use;

import com.a.Dog;

public class Use {
    public static void main(String[] args) {
        Dog da = new Dog();
        System.out.println(da);

        com.b.Dog db = new com.b.Dog();
        System.out.println(db);
    }
}

It can be found that when using the first a package, it is automatically imported, and when using the b package,

He brought com.b up front.

3. Package naming convention

Generally speaking, it is:

com.company name.project name.business module name.

4. Commonly used packages

The java.lang base package is included automatically.

java.util Contains tools commonly used by the system, such as Scanner.

java.net networking package, web development

java.awt for java page development, GUI 

5. Access modifiers

1. Modified with public, open to the public

2. Modified with protected, open to subclasses and classes in the same package

3. Default (not modified), open to classes in the same package

4. Decorate with private, only the class itself can access it, not open to the public

Lao Han has a picture:

6. Inheritance 

Inherited syntax:

class subclass extends parent class {

}

Notice:

1) The subclass will automatically have the properties and methods defined by the parent class (provided it is modified by public and protected)

2) The parent class is also called the super class, the base class.

3) Subclasses are also called derived classes.

Let’s write a piece of code here as an example~

public class Extend {
    public static void main(String[] args) {
        Student student = new Student();
        student.Speak();
    }
}

class Person {
    protected String _name;
    protected int _age;
    protected int _money;

    public void Speak() {
        System.out.println("hello exthend!");
    }
}

class Student extends Person {
    private int _id;
}

The subclass Student inherits from the parent class Person.

Inheritance related details:

1) The subclass object will call the default constructor of the parent class by default. If you want to specify it, you need to use super

for example:

public class Extend {
    public static void main(String[] args) {
        Student student = new Student();
    }
}

class Person {
    protected String _name;
    protected int _age;
    protected int _money;
    Person() {
        System.out.println("Person()");
    }

    Person(String name) {
        _name = name;
        System.out.println("Person(String name)");
    }
    public void Speak() {
        System.out.println("hello exthend!");
    }
}

class Student extends Person {
    private int _id;
    public Student() {
        super("张三");
        System.out.println("Student()");
    }
}

Here, super can be used to specify the parameterized structure of calling the parent class.

2) Both super() and this() can only be placed on the first line of the constructor (they cannot coexist)

this() allows the default constructor to call other overloaded constructors.

3) Use Ctrl + h to see the inheritance relationship of the class. Note that all classes are subclasses of the Object class

Check out the demo:

4) Subclasses can only inherit at most one parent class (java is a single inheritance system)

So what if we want class A and class B?

You can let class A inherit class B first, and then we can also inherit class A. 

7. The super keyword

super represents a reference to the parent class, used to access the properties, methods, and constructors of the parent class

super. Attribute name; can access parent class attributes

super. method name (parameter list); can access the parent class method

super (parameter list); can access the parent class constructor (can only be placed on the first line of the constructor)

Details to pay attention to:

1) If there is a method with the same name, you need to use super to call it. If there is no method with the same name, just use it directly.

2) The access of super is not limited to the parent class, it can also be above the grandpa class, but super follows the principle of proximity.

Here's how super compares to this:

8. Method rewriting Override

Java's method rewriting is the same as C++'s,

That is, the parameters, return value, and method name of the subclass method are the same as the parent class method, which is method rewriting.

There are some caveats though:

1) Subclass methods cannot narrow the access rights of parent class methods.

2) Analysis of the difference between method rewriting and overloading:

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/132046583