java Foundation (14): Use of Eclipse, Object-Oriented, Custom Data Types

1. Application of Eclipse

1. Common shortcuts

Ctrl+T: View the inheritance tree of the selected class

For example, in the following code, select the Teacher class name and press Ctrl+T to show the inheritance relationship of the Teacher class

 

 

 

//staff 
abstract class Employee{
    public abstract void work();
}

//lecturer
class Teacher extends Employee {
    public void work() {
        System.out.println("Explaining Java");
    }
}

View the source code for the selected class

Ctrl + slide the mouse to click on the class name, or after selecting the class name, press F3 to view the source code of the selected class.

 

 

 

View the source code of the selected method

Ctrl + slide the mouse to click on the method name, or after selecting the method name, press F3 to view the source code of the selected method.

 

 

 

The JRE System Library in Eclipse is the default Eclipse dependency class library in JRE. In this location, you can find commonly used String classes, Random classes, Math classes, and so on.

1.2 Document Annotations Export Help Documents

When using eclipse, you can export the description documents of classes with document annotations, so that others can read, learn and use them.

By using document annotations, classes or methods are annotated with @ simple annotation of basic information. For example, @author, @version code version, @param method parameter, @return method return value, etc.

package cn.itcast;
/**
 * My Tool Class
 * @author Li
 * @version 1.0 Edition
 */
public class Tool {
    /**
     * Returns the cumulative sum of two integers
     * @param num1 First number
     * @param num2 Second numbers
     * @return Return the cumulative sum
     */
    public static int getSum(int num1, int num2){
        return num1 + num2;
    }
}

Using Eclipse to export javadoc documents can be done as follows:

1. Right click the item

2. Select Export

3. Click on javadoc in the java directory

4. Choose the export path

5. Click finish

jar package import and export for project 1.3

The jar package is a compressed file that can contain many. class files. We can add a jar package to the dependencies of the project so that the project can use all the classes under the jar, or we can package all the classes in the project into the specified jar package for other projects to use.

Export jar packages: All classes in the project are packaged into the specified jar packages.

Import the jar package: that is, add the specified jar package to the project and provide it for use by the project.

The process of importing the jar package is to add the jar package to the project's. classpath file so that the project can be identified and all. class file classes in the jar package can be used. The following are the steps to join:

1: Create a lib folder under the project root folder to manage all jar files in the same way

2: Copy the jar file to the lib folder

3: Right-click on the jar file, click Build Path, select Add to Build Path, and then look at the. classpath file under the project root folder, and find that the newly added jar package path is configured in the file. Explains that you can use all the classes in the jar package.

Be careful:

After the Jar package is added, you must Add to Build Path to use it

After the Jar package is added, the added class must also be the guide package. If the added class has the same package name as the existing class package name, it will be treated as the same package. (not common)

2. Object-oriented

2.1 Use details of different modifiers

The modifiers commonly used to modify classes, methods, and variables are as follows:

Public permission modifiers, public access, classes, methods, member variables

Protected permission modifiers, protected access, methods, member variables

By default, nothing is written as a permission modifier, default access, class, method, member variable.

Private permission modifiers, private access, methods, member variables

Static static modifier method, member variable

final modifier class, method, member variable, local variable

Abstract abstract modifier class, method

When we write programs, permission modifiers are generally placed before all modifiers, and different permission modifiers cannot be used at the same time.

At the same time, abstract and private can not be used at the same time.

At the same time, abstract and static can not be used simultaneously.

At the same time, abstract and final cannot be used simultaneously.

Modifiers that a modifier class can use:

Modified classes can only use public, default, final, abstract keywords

The most frequently used keyword is public

public class Demo {} //The most commonly used way
class Demo2{}
public final class Demo3{}
public abstract class Demo4{}

Modifiers that can be used to modify member variables:

public: public

protected

Default

private: private

final: final

static: static

private is the most frequently used

public int count = 100;
protected int count2 = 100;
int count3 = 100;
private int count4 = 100; //The most commonly used way
public final int count5 = 100;
public static int count6 = 100;

The modifier that the modifier constructor can use:

public: public

protected

Default

private: private

The most frequently used is public.

public Demo(){} //The most commonly used way
protected Demo(){}
Demo(){}
private Demo(){}

l. Modifiers that can be used by member methods:

public: public

protected

Default

private: private

final: final

static: static

abstract: abstract

The most frequently used is public.

public void method1(){}//The most commonly used way
protected void method2(){}
void method3(){}
private void method4(){}
public final void method5(){}
public static void method6(){}//The most commonly used way
public abstract void method7();//The most commonly used way

3. Use of custom data types

3.1 Design Definition of Membership Variables and Method Parameters

Define rectangular classes, including methods for finding circumference and area

Define a class of mathematical tools, including methods to double the sum of two numbers and to find the product of two numbers

Thinking: the calculation methods of these two classes need two numbers to participate in the calculation. Will the two number definition be better in the member position or the parameter position? Why?

If a variable is part of this class, it is defined as a member variable.

A variable is defined as a parameter variable if it is not a part of a class but merely a number of functions that need to be calculated.

Mathematical Tools

public class MathTool {
    //Double the sum of two numbers
    public double sum2times(int number,int number2) {
        return (number+number2)*2;
    }
    //Find the product of two numbers
    public double area(int number,int number2) {
        return number*number2;
    }
}

Rectangular class

public class CFX {
    //Because length and width are part of things in reality, member variables are defined.
    private int chang;
    private int kuan;
    
    public CFX(int chang, int kuan) {
        this.chang = chang;
        this.kuan = kuan;
    }
    
    //Finding the Circumference of Length and Width
    public double zhouChang() {
        return (chang+kuan)*2;
    }
    //Finding the Area of Length and Width
    public double mianJi() {
        return chang*kuan;
    }
    public int getChang() {
        return chang;
    }
    public void setChang(int chang) {
        this.chang = chang;
    }
    public int getKuan() {
        return kuan;
    }
    public void setKuan(int kuan) {
        this.kuan = kuan;
    }
}

Class 3.2 as method parameter and return value

Class as method parameter

In writing programs, we often encounter the case that the method to be invoked receives a class type. Then, we need to pass in the object of that class to the method. The following code demonstrates:

class Person{
    public void show(){
        System.out.println("show Method executed");
    }
}
//Test class
public class Test {
    public static void main(String[] args) {
        //Establish Person object
        Person p = new Person();
        //call method Method
        method(p);
    }
    
    //Define a method method,To receive a Person Object, called in a method Person Object show Method
    public static void method(Person p){
        p.show();
    }
}

Class as method return value

When writing a program to call a method, we will often encounter the return value of a class type in the future, then the method will return an object of that class. The following code demonstrates:

class Person{
    public void show(){
        System.out.println("show Method executed");
    }
}
//Test class
public class Test {
    public static void main(String[] args) {
        //call method Method to get the returned Person object
        Person p = method();
        //call p Object show Method
        p.show();
    }
    
    //Define a method method,Used to get a Person Object, completed in method Person Object creation
    public static Person method(){
        Person p = new Person();
        return p;
    }
}

3.3 Abstract classes as method parameters and return values

Abstract classes as method parameters

In the future development, abstract classes as method parameters are also very common. When a method parameter is an abstract class type, a subclass object that implements all abstract methods of the abstract class is passed in. The following code demonstrates:

//abstract class
abstract class Person{
    public abstract void show();
}
class Student extends Person{
    @Override
    public void show() {
        System.out.println("Rewrote show Method");
    }
}
//Test class
public class Test {
    public static void main(String[] args) {
        //Create one through polymorphism Person A variable of type, and this object is actually Student
        Person p = new Student();
        //call method Method
        method(p);
    }
    
    //Define a method method,To receive a Person Type object, called in method Person Object show Method
    public static void method(Person p){//Abstract classes as parameters
        //adopt p Variable call show Method,What is actually called at this point is Student Object show Method
        p.show();    
  }
}

Abstract classes as method return values

There is also the case where abstract classes are returned as method values, which requires returning a subclass object that implements all abstract methods of abstract classes. The following code demonstrates:

//abstract class
abstract class Person{
    public abstract void show();
}
class Student extends Person{
    @Override
    public void show() {
        System.out.println("Rewrote show Method");
    }
}
//Test class
public class Test {
    public static void main(String[] args) {
        //call method Method to get the returned Person object
        Person p = method();
        //adopt p Variable call show Method,What is actually called at this point is Student Object show Method
        p.show();
    }
    
    //Define a method method,Used to get a Person Object, completed in method Person Object creation
    public static Person method(){
        Person p = new Student();
        return p;
    }
}

3.4 Interface as Method Parameter and Return Value

Interface as method parameter

Interface as a method parameter is very common and often encountered. When a method parameter is an interface type, the method passes in an interface implementation class object. The following code demonstrates.

//Interface
interface Smoke{
    public abstract void smoking();
}
class Student implements Smoke{
    @Override
    public void smoking() {
        System.out.println("Smoking after class is better than living immortals.");
    }
}
//Test class
public class Test {
    public static void main(String[] args) {
        //Create one through polymorphism Smoke A variable of type, and this object is actually Student
        Smoke s = new Student();
        //call method Method
        method(s);
    }
    
    //Define a method method,To receive a Smoke Type object, called in method Smoke Object show Method
    public static void method(Smoke sm){//Interface as a parameter
        //adopt sm Variable call smoking Method, when the actual call is Student Object smoking Method
        sm.smoking();
    }
}

Interface as method return value

Interface as method return value will be encountered in later learning. When a method return value is an interface type, the method needs to return an interface implementation class object. The following code demonstrates.

//Interface
interface Smoke{
    public abstract void smoking();
}
class Student implements Smoke{
    @Override
    public void smoking() {
        System.out.println("Smoking after class is better than living immortals.");
    }
}
//Test class
public class Test {
    public static void main(String[] args) {
        //call method Method: Get the returned smokers
        Smoke s = method();
        //adopt s Variable call smoking Method,What is actually called at this point is Student Object smoking Method
        s.smoking();
    }
    
    //Define a method method,It is used to acquire an object with smoking function and complete the creation of smokers in the method.
    public static Smoke method(){
        Smoke sm = new Student();
        return sm;
    }
}

Tags: Java Eclipse

Posted on Wed, 09 Oct 2019 18:29:17 -0700 by McMaster