day09 package / eclipse uses / this / inherit / rewrite / super

Posted by jdadwilson on Fri, 07 Jan 2022 17:43:56 +0100

catalogue

1, Package

1. Function of package:

2. How to declare a package?

3. How to compile packaged classes

4. How to run a packaged class

5. How to use the class of another package?

2, eclipse overview and basic interface

1. Installation steps

2. New project

3, Using eclipse: colors and fonts

4, Using eclipse: running and character encoding

1. Run project

2. Unified coding format

5, Using eclipse: shortcut keys

6, Using eclipse: rapid development

7, Keyword this

8, Using eclipse: importing projects and deleting

9, The second basic feature of object-oriented: inheritance (1)

10, The second basic feature of object-oriented: inheritance (2)

11, Method override

12, Inherited exercises

13, Keywords: super

1, Package

1. Function of package:

(1) Avoid class name duplication

With a package, the name of the class is longer, and the full name of the class becomes: package. Class name

For example:

java.util.Scanner -- the full name of the scanner class

java.lang.String -- full name of String class

java.lang.System -- full name of System class

java.lang.Math -- full name of Math class

(2) The classification organization manages a large number of classes

java.io - related to IO (input / output)

java.util -- related to various common tool classes

java.lang -- core system class

java.net -- related to network programming

java.sql -- related to database programming

(3) For access control

If the permission modifier of a class or method is omitted, it is only used in this package

2. How to declare a package?

Syntax format:

package Package name;

This sentence requires:

(1) One java source files can only have one sentence

(2) Must be on the first line of code in the source file

Naming conventions for packages:

(1) All words are in lowercase. Use between each word division

(2) It is customary to invert the company's domain name + module name

For example:

com.atguigu.xx

com.mysql.xxx

com.alibaba.xxx

org.aphache.xxx

Primary domain name:

com: business

org: non profit organization

gov: government

edu: Education

......

3. How to compile packaged classes

For example:

Compilation: javac -d Source file name java

4. How to run a packaged class

java package Class name

5. How to use the class of another package?

Class of another package:

package com.moumou.bean;

class Circle{
	private double radius;
	Circle(){
	}
	Circle(double r){
	radius=r;
	}
	void printInfo(){
		System.out.println("radius"+radius);
	}
}

Compile it: you can only compile it and cannot run it, because there is no main method and no entry

Folder after compilation:

First:

Compilation: the error that the symbol cannot be found is reported because we did not add the package name

Second: add the package name

Compilation: cannot access another package

Prefix the class of another package with the public modifier

Compilation: therefore, the constructor and method should be preceded by the public modifier

After the public modifier is added in front of constructors and methods:

After the above discovery, if the class with another package is always a package The writing method of class name will be cumbersome, so you can use import statement + Short Name:

Summary:

Premise: the permission modifier of the used class or member must allow cross package use

Method:

(1) Use full name: package Class name

(2) Use import statement + simple name

explain:

(1) The import statement is written between the package statement and the class declaration

(2) There can be many import package statements to import classes of different packages

(3)java. Classes in Lang package can directly use simple names without using import statements

For example: System, String, Math

(4) You can import many classes in the same package at the same time

Improve package*

For example:

(5) if there are different packages, but the class names are the same

For example:

java.util.Date

java.sql.Date

Compile:

Solution: therefore, only one can choose to use the full name and the other can use the simple name. You can't use the simple name at the same time.

2, eclipse overview and basic interface

Download URL: https://www.eclipse.org/downloads/

1. Installation steps

 workspace:

I create a new folder on disk D:

After entering the interface:

We originally wrote java code in notpad + +, and the command-line tool compiled and ran the code. Now we can use the eclipse development tool to:

At present, our interface looks like the following:

If you want to come in like this next time:

 

Then, the last layout will be opened in the new Window:

 

2. New project

1. New java project

 

 

2. Create a new package

 

3. Create a class file under the package

In this way, the package and class can be established at the same time:

3, Using eclipse: colors and fonts

1. Adjust the font size of the interface:

 

effect:

2. Change the code of the editing area

Effect:

Colors like the following can also be set:

In the above figure, the contents of the circle and the circle are set correspondingly, and the rectangle and the arrow are set correspondingly.

In addition, you can also select a theme:

design sketch:

4, Using eclipse: running and character encoding

1. Run project

There is no need to compile the code written with this eclipse tool. After the code is written and saved, it will be compiled automatically.

How does it work?

In addition:

 

2. Unified coding format

5, Using eclipse: shortcut keys

 

 

First tip: new local variables

Second tip: generate an attribute

 

If you want to change the class name, you can also use Ctrl+1

 

Quick package guide operation:

Copy the above code into Eclipse:

Then the package is imported:

 

To view the source code file of a class:

 

6, Using eclipse: rapid development

 

 

Effect:

Renderings:

 

Select the rendering of Generate Getters and Setters:

7, Keyword this

1. Meaning: current object

(1) Constructor: the object being created

(2) Method: the object that is calling the method

2. Usage

(1)this. attribute

When a local variable has the same name as a member, you can add this before the member variable

 (2)this. Method: when it's useless, it's necessary to use it

(3) this() or this (argument list)

this() means to call the parameterless construction of this class

This (argument list) indicates that the parameterized construction of this class is called

this() or this (argument list) must be in the first line of the constructor

8, Using eclipse: importing projects and deleting

 

 

 

9, The second basic feature of object-oriented: inheritance (1)

Two meanings of inheritance:

(1) Continuation

(2) Expand

1. Why inheritance?

(1) When a class, it needs to derive many subcategories

Person class: you need to derive subcategories such as Teacher and Student

At this time, the common part in Person does not need to be declared again in the subcategory

(2) When multiple classes have common features, the common parts can be extracted into the parent class

2. Purpose

Code reuse and extension

3. How to implement inheritance?

[Modifier] class Subclass extends Parent class{
}

Subclass: subclass, also known as derived class

Parent class: superclass, also known as superclass and base class

4. Characteristics of inheritance:

(1) The child class inherits the parent class,

In terms of the characteristics of things, the subclass will inherit all the characteristics (properties and methods) of the parent class

However, from the perspective of code operation, the private properties and methods in the parent class cannot be used directly in the child class

Example code:

package com.moumou.test;

public class TestThis{
	public static void main(String[] args) {
		Teacher t=new Teacher();
		t.setName("Cutie");
		t.setAge(18);
		System.out.println("full name:"+t.getName());
		System.out.println("Age:"+t.getAge());
	}
}
class Person{
	private String name;
	private int age;
	
	public Person() {
		super();
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	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;
	}
}
class Teacher extends Person{
	private double salary;
	public void test(){
		System.out.println("name = " + name);//Error: the private attribute of the parent class cannot be used directly in the child class
	}
	
}

In the appeal code, Person is the parent class, Teacher is the child class, inherits the parent class, and the parent class has the name attribute, but because the name attribute is private, the child class cannot be used directly!

(2) When a subclass inherits from the parent class, the constructor cannot be inherited

Example code:

package com.moumou.test;

public class TestThis{
	public static void main(String[] args) {
		Teacher t=new Teacher();
		t.setName("Cutie");
		t.setAge(18);
		System.out.println("full name:"+t.getName());
		System.out.println("Age:"+t.getAge());
		
		Teacher t1=new Teacher("Little fool",19);//report errors
	}
}
class Person{
	private String name;
	private int age;
	
	public Person() {
		super();
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	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;
	}
}
class Teacher extends Person{
	private double salary;
	public void test(){
		//System.out.println("name = " + name);// Error: the private attribute of the parent class cannot be used directly in the child class
	}
	
}

Note: in the appeal, the parent class has a constructor with or without parameters and a constructor with parameters, but when the child class inherits the parent class, the constructor cannot be inherited. All appeal codes Teacher t1=new Teacher("little fool", 19); Will report an error

(3) When a subclass inherits from the parent class, the constructor of the parent class must be called in the constructor of the subclass

By default, the parameterless construction of the parent class is called

If the parent class does not have a parameterless construct, the parameterless construct of the parent class must be manually called in the first line of the constructor of the child class;

The code is as follows:

package com.moumou.test;

public class TestThis{
	public static void main(String[] args) {
		Teacher t=new Teacher();
		t.setName("Cutie");
		t.setAge(18);
		System.out.println("full name:"+t.getName());
		System.out.println("Age:"+t.getAge());
		
		//Teacher t1=new Teacher("little fool", 19)// report errors
	}
}
class Person{
	private String name;
	private int age;
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	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;
	}
}
class Teacher extends Person{
	private double salary;
	public void test(){
		//System.out.println("name = " + name);// Error: the private attribute of the parent class cannot be used directly in the child class
	}
}

In the appeal code, the Teacher class inherits the Person class, but the Person class has no parameterless structure, so an error will be reported:

Therefore, there are two solutions: either add a parameterless construct in the parent class or manually call the parameterless construct of the parent class in the first line of the constructor of the child class

give an example:

super(name,age): called the parameterized construction of the parent class

package com.atguigu.test02;

/*
 * Inheritance:
 * (1)continue	
 * (2)extend
 * 
 * 1,Why inheritance?
 * (1)When a class, it needs to derive many subcategories
 * Person: 
 * 	  You need to derive subcategories such as Teacher and Student
 * 	 At this time, the common part in Person does not need to be declared again in the subcategory
 * (2)When multiple classes have common features, the common parts can be extracted into the parent class
 * 
 * Purpose:
 * 	Code reuse and extension
 * 
 * 2,How to implement inheritance?
 * [Modifier] class subclass extends parent class{
 * }
 * 
 * Subclass: subclass, also known as derived class
 * Parent class: superclass, also known as superclass and base class
 * 
 * 3,Characteristics of inheritance:
 * (1)The child class inherits the parent class,
 * In terms of the characteristics of things, the subclass will inherit all the characteristics (properties and methods) of the parent class.
 * However, from the perspective of code operation, the private properties and methods in the parent class cannot be used directly in the child class
 * (2)When a subclass inherits from the parent class, the constructor cannot be inherited
 * (3)When a subclass inherits from the parent class, the constructor of the parent class must be called in the constructor of the subclass
 * By default, the parameterless construction of the parent class is called;
 * If the parent class does not have a parameterless construct, the parameterless construct of the parent class must be manually called in the first line of the constructor of the child class;
 * 
 * Unfinished to be continued...
 */
public class TestInherited {
	public static void main(String[] args) {
		Teacher t = new Teacher();
		t.setName("Teacher Chai");
		t.setAge(18);
		
		System.out.println("full name:" + t.getName());
		System.out.println("Age:" + t.getAge());
		
//		Teacher t2 = new Teacher("teacher song", 28);
	}
}
class Person{
	private String name;
	private int age;
	//constructor 
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	//get/set
	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;
	}
	
}
class Teacher extends Person{
	private double salary;
	
	public Teacher() {
		super(null, 0);
	}

	public Teacher(String name, int age, double salary) {
		super(name, age);//Calling a parameterized construct of the parent class must be in the first line of the subclass constructor
		this.salary = salary;
	}

	public void test(){
		//System.out.println("name = " + name);// The private property of the parent class cannot be used directly in the child class
	}
}
class Student extends Person{
	private int score;

	public Student(String name, int age, int score) {
		super(name, age);//Calling a parameterized construct of the parent class must be in the first line of the subclass constructor
		this.score = score;
	}
	
}

10, The second basic feature of object-oriented: inheritance (2)

 * (1)The child class inherits the parent class,
 * In terms of the characteristics of things, the subclass will inherit all the characteristics (properties and methods) of the parent class.
 * However, from the perspective of code operation, the private properties and methods in the parent class cannot be used directly in the child class
 * (2)When a subclass inherits from the parent class, the constructor cannot be inherited
 * (3)When a subclass inherits from the parent class, the constructor of the parent class must be called in the constructor of the subclass
 * By default, the parameterless construction of the parent class is called;
 * If the parent class does not have a parameterless construct, the parameterless construct of the parent class must be manually called in the first line of the constructor of the child class;
 * (4)Java Only single inheritance is supported, that is, one Java Class can only have one direct parent
 * 		There can only be one biological father
 * (5)Java Multi level inheritance is supported, that is, the parent class can also have a parent class
 * 		pass on generation after generation
 * (6)One Java Class can have many subclasses at the same time, and subclasses can also have subclasses
 * 		A father can have many children and grandchildren
 * (7)Subclasses can extend properties and methods that the parent class does not have

Example code:

The inheritance of java can only be single inheritance and cannot have two parent classes, but we do this indirectly:

11, Method override

Keyword: Override

When the subclass inherits the method of the parent class, but the method body of the parent class is not applicable to the subclass, the subclass can choose to "overwrite";

Method = method signature / method header + method body

give an example:

package com.atguigu.test02;

/*
 * Override of method: override
 * When a subclass inherits the method of the parent class, but the method body of the parent class is not applicable to the subclass, the subclass can choose to overwrite.
 * 
 * Method = method signature / method header + method body
 * 
 * Rewriting requires:
 * (1)Method name: must be the same as the method name overridden by the parent class
 * (2)Formal parameter list: must be the same as the formal parameter list overridden by the parent class
 * (3)Return value type:
 * 	   Basic data type and void: it is required to be the same as the return value type of the method whose parent class is overridden
 *   Reference data type: the return value type of the method requiring subclass override < = the return value type of the method whose parent class is overridden
 *   		For example:
 *   			The return value type of the subclass method is Student, and the return value type of the overridden method of the parent class is Student
 *   			The return value type of the subclass method is Student, and the return value type of the overridden method of the parent class is Person
 *   			The return value type of the subclass method is Person, and the return value type of the overridden method of the parent class is Student (wrong)
 * (4)Modifier 
 * ①Permission modifier: visibility range of permission modifier of method overridden by subclass > = visibility range of permission modifier of method overridden by parent class
 * 			For example:
 * 				The permission modifier of the subclass method is public, and the permission modifier of the overridden method of the parent class is public
 * 				The permission modifier of the subclass method is public, and the permission modifier of the overridden method of the parent class is protected
 * ②Other modifiers (later)
 */
public class TestOverride {
	public static void main(String[] args) {
		Manager m = new Manager("Cui Zhiheng", 20000,1000);
		System.out.println(m.getInfo());
	}
}
//staff
class Employee{
	//Attribute list
	private String name;
	private double salary;
	//Constructor list
	public Employee(String name, double salary) {
		this.name = name;
		this.salary = salary;
	}
	public Employee() {
	}
	//get/set
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public String getInfo(){
		return "full name:" + name + ",Salary:" + salary;
	}
}
//manager
class Manager extends Employee{
	//Property list of subclasses
	private double bonus;//bonus

	//List of constructors for subclasses
	public Manager(String name, double salary, double bonus) {
		super(name, salary);
		this.bonus = bonus;
	}

	public Manager() {
	}

	//get/set of subclasses
	public double getBonus() {
		return bonus;
	}

	public void setBonus(double bonus) {
		this.bonus = bonus;
	}
	
	//Override getInfo() of parent class
	public String getInfo(){
		//The name and salary attributes of the parent class are private and cannot be used directly in the child class
		//return "Name:" + name + ", salary:" + salary + ", bonus" + bonus;
		return "full name:" + getName() + ",Salary:"+ getSalary() + ",bonus" + bonus;
	}
}

12, Inherited exercises

13, Keywords: super

Find from the parent class and reference xx of the parent class

 

Usage:

(1)super. attribute

When a subclass declares an attribute with the same name as the parent class, you can use super Property to access the properties of the parent class

(2)super. method

(3) super() or super (argument list)

give an example:

 

package com.atguigu.test02;

/*
 * super: 
 * 	  Find from the parent class and reference xx of the parent class
 * Requirement: super method can only be used for attributes, methods and constructors visible in subclasses
 * 
 * Usage:
 * 1,super.attribute
 * When a subclass declares an attribute with the same name as the parent class, you can use super Property to access the properties of the parent class
 * 
 * 2,super.method
 * When you need to call the overridden method of the parent class in a subclass, you can use super method
 * 
 * 3,super()Or super (argument list)
 * super();Call the parameterless construction of the parent class
 * super(Argument list): call the parameterized construction of the parent class
 * 
 * be careful:
 * (1)super()Or super (argument list) must be in the first line of the subclass constructor
 * (2)If super() is not written in the constructor of the subclass, it also exists
 *    However, if super (argument list) is written in the subclass constructor, super() will not exist
 */
public class TestSuper {
	public static void main(String[] args) {
//		B b = new B();
//		b.printNum(3);
		
//		XueSheng x = new XueSheng("Zhang San", 23, 89);
//		System.out.println(x.getInfo());
		
		XueSheng x = new XueSheng();
	}
}
class A{
	int num = 1;
}
class B extends A{
	int num = 2;
	public void printNum(int num){
		System.out.println(num);//Local variable (int num)
		System.out.println(this.num);//Member variable, subclass's own int num
		System.out.println(super.num);
	}
}
class Human{
	private String name;
	private int age;
	public Human(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Human() {
		super();//Calling the public parent class java Nonparametric construction of lang.Object class
		System.out.println("Parameterless construction of parent class");
	}
	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 String getInfo(){
		return "full name:" + name + ",Age:" + age;
	}
}
class XueSheng extends Human{
	private int score;

	public XueSheng(String name, int age, int score) {
		super(name, age);//Call the parameterized constructor of the parent class Human
		this.score = score;
	}

	public XueSheng() {
//		super();// Call the parameterless construction of the parent class Human
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
	public String getInfo(){
	//	return "Name:" + getName() + ", age:" + getAge() + ", score:" + score;
		return super.getInfo() + ",Achievement:" + score;
	}
}

Topics: Java Eclipse IDE