Package and inheritance
Package
It is a way to organize classes.
The main purpose of using packages is to ensure the uniqueness of classes.
Package specification
The domain name is generally inverted , and all lowercase , for example: www.baidu.com
so the package can be set like this
Import package
Use the import keyword, followed by the package you want to import
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
// 得到一个毫秒级别的时间戳
System.out.println(date.getTime());
}
}
Access to the package
- public
- private
- protected
- Do not write anything (default permission)———— can only be accessed in the same package
Common system packages
- java.lang: Basic classes (String, Object) commonly used in the system, this package is automatically imported from JDK1.1.
- java.lang.reflect: java reflection programming package;
- java.net: Development kit for network programming.
- java.sql: Support package for database development.
- java.util: is the tool package provided by java. (Collections, etc.) very important
- java.io: I/O programming development kit.
Encapsulation
What is encapsulation?
Use private to modify fields or methods.
The significance of encapsulation lies in:
safety, so that the caller of the class reduces the use cost of the class.
Detailed package before I wrote the essay in class and object
point I jump classes and objects
inherit
Significance: reduce code reuse
extends keyword
A extends B
means A inherits B
At this time, B is called base class/parent class/super class
and A is called subclass/derived class
For example,
Dog inherits Animal
Bird inherits Animal
class Animal {
public String name;
public int age;
public void eat() {
System.out.println("Animal::eat()");
}
}
class Dog{
public String name;
public int age;
public void eat() {
System.out.println("Dog::eat()");
}
}
class Bird{
public String name;
public int age;
public void eat() {
System.out.println("Bird::eat()");
}
public void fly() {
System.out.println("Bird::fly()");
}
}
We only need to let Dog and Bird extend Animal no longer need to write repeated fields and methods, like this
class Animal {
public String name;
public int age;
public void eat() {
System.out.println(this.name + "::eat()");
}
}
class Dog extends Animal{
}
class Bird extends Animal{
public void fly() {
System.out.println("Bird::fly()");
}
}
So the question is, what do the
child classes inherit from the parent class?
inheritIn addition to the construction methodThe content of all parent classes. Contains private-modified fields or methods,
but it should be noted that private-modified cannot be accessed in subclasses.
When is the relationship set to inherit?
A is B Inheritance needs to be used at this time. For
example, in the above example, a dog is an animal, and a bird is an animal,
but it cannot be a dog or a bird. In this way, inheritance is not needed.
Java is single inheritance, that is, only one class can be extended.
In order to solve the single inheritance problem, java introduces interfaces. We will continue to learn about interfaces later.
protected keyword
Just now we found that if the field is set to private, the subclass cannot be accessed. But setting it to public violates our original intention of "encapsulation". The
best of both worlds is the protected keyword.
- For the caller of the class, protected fields and methods cannot be accessed
- For the class Subclass with Other classes in the same package In other words, protected modified fields and methods are accessible
//在animal包中
class Animal {
protected String name;
public int age;
public void eat() {
System.out.println(this.name + "::eat()");
}
}
//在test包中
class Test extends Animal{
public void func(){
Animal animal = new Animal();
System.out.println(animal.name);//错误!!这样调用时不行的
System.out.println(super.name);//正确,调用父类的需用到super关键字
}
}
Summarize access-restricted keywords
final keyword
Before we learned that final can modify characters
final int a = 10;
final can also modify the class
final class A {
}
This means that the class A is a sealed class , which means that the current class cannot be inherited.
If you do not want to be inherited by other classes when setting up the class later, you can use final modification
super keyword
super and this are often asked together,
this stands forReference to the current object
Let me talk about several methods of this first
- this() calls its own constructor
- this.func() access method
- this.data access attribute
- this represents a reference to the current object
super means to getReference to an instance of the parent class
The subclass inherits the parent class. When constructing the subclass, you need to firstHelp the parent class to construct
How to help the construction, inside the construction method of the subclass,Call the constructor of the parent class
super(); //Display the construction method of calling the parent class. Therefore, the constructor is not inherited, but is called in the subclass
class Animal {
public String name;
public int age;
public Animal1() {
}
public Animal1(String name){
}
public void eat() {
System.out.println(this.name + "::eat()");
}
}
class Dog extends Animal{
public Dog() {
super();
}
public Dog(String name) {
super(name);
}
}
Note:super cannot be used in static methods, Because the static modification method does not rely on any object, and super itself represents a reference to the parent class object.
Parent-child class execution order
class Animal {
public String name;
public int age;
//静态代码块
static {
System.out.println("Animal::static{}");
}
//实例代码块
{
System.out.println("Animal::{}");
}
public Animal() {
System.out.println("Animal()");
}
public Animal(String name){
System.out.println("Animal(String)");
}
public void eat() {
System.out.println(this.name + "::eat()");
}
}
class Dog extends Animal1{
static {
System.out.println("Dog::static{}");
}
{
System.out.println("Dog::{}");
}
public Dog() {
super();
System.out.println("Dog()");
}
public Dog(String name) {
super(name);
System.out.println("Dog(String)");
}
}
class Bird extends Animal1{
static {
System.out.println("Bird::static{}");
}
{
System.out.println("Bird::{}");
}
public Bird() {
System.out.println("Bird()");
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println("========");
Bird bird = new Bird();
}
}