JavaSE object-oriented basics_inheritance

inherit

Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits the methods from the parent class, so that the subclass has the same behavior as the parent class.

Inheritance is a technology that uses the definition of an existing class as the basis to create a new class. The definition of a new class can add new data or new functions, or use the functions of the parent class, but it cannot selectively inherit the parent class.

Inheritance is a mutual relationship of is-a

Inherited advantages

  • Put the common attributes of all subclasses into the parent class to realize code sharing, avoid duplication, and improve development efficiency
  • Can make it easier to modify the implementation inherited from the extension

Inherited defects

  • When the parent class changes, the child class must change
  • Inheritance breaks encapsulation. For the parent class, its implementation details are transparent to the subclass.
  • Inheritance is a strong coupling relationship

special keywords

package package

A package is a container for classes or interfaces, a namespace used to avoid naming conflicts, and a logical naming concept that has nothing to do with specific physical locations

In a specific implementation, the package name often corresponds to the directory name

what is a package

  • A package is an organization of classes, providing namespaces and access control
  • filesystem-like organization

Declare classes in package package

grammar:package 包名称;

  • Declaring a package requirement is the first statement of a source code file. Annotation information can be added before the package definition
  • There can be at most one package statement in a program, and source code files without a package definition become part of the default package
//默认包中的类可以访问其它命名包或者只能同包访问,其它包不能访问默认包中的类
public class A1 {
    
    
	public void cc() {
    
    
		System.out.println("小胖子醒醒!");
	}
}
  • If there is a package statement in the program, it must be the first statement (there can only be comments or blank lines in front of it)
  • Package naming rules:
    • all lowercase
    • Use the method of domain name reversal to define, for example, for a project of a group xxxx.com, define the package name as: com.xxxx
      • If the subdivision can also introduce the function definition part, such as database access com.yanan.dao

The role of the package

  • Packages group related source code files together
    • com.yanan.dao stores the class files related to the operation database
    • com.yanan.biz stores related business files
  • Packages reduce problems with name collisions
  • You can use package to indicate which specific package the class in the source file belongs to

package use

  • If other people want to use the class in the package, they need to use the full name
import java.util.Date;
public class A{
    
    
public static void main(String[] args){
    
    
Date now=new Date(); //使用类的简称Date,而不是全名称java.util.Date
}
}
//用法2:全称
java.util.Date now=new java.util.Date(); //不需要import
  • To simplify writing, Java provides the import statement:import package_name.*;
import java.util.*;
//使用java.util包中的所有类都不需要再使用import java.util.类名
  • Import a class in the package:import java.util.ArrayList;

Import package import

  • The package imported by Java by default is java.lang
System.out.println("显示输出");//不需要import java.lang.System
//同包中的其它类不需要导入
package com.yan;
public class A{
    
    }
package com.yan;
public class B{
    
    
public void pp(){
    
    
A a=new A1(); //不需要import com.yan.A
}
}
  • Import the classes in the package: import package name. class name; for example import java.util.Date;
  • import the whole package; import 包名称.* ;eg import java.util.*;
  • If you use a class instead of a package, you need to use the full name of the class: package name.class name, for example java.util.Date dd=new java.util.Date();

package description

package-info.java can describe the role of a package

//这是一个测试包
package com.yan.test;

Public and private classes cannot be defined in package-info, but friendly classes and package constants can be defined, and provided to the classes under the unified package for sharing

package com.yan.a; 
import lombok.Data; 

class Constants{
    
     
	public static final String NAME="Yanjun"; 
}
@Data 
class Teacher{
    
     //包中的类可以直接使用 
	private String className; 
}

Finally, for the package scope of annotations, we will continue to explain later

Scope qualifiers and packages

//定义不包含再其它类中的外部类时,class前的范围限定词可以使用public或者默认 
//public 到处可见 没有限定词只能同包访问 
package com.bao; 

public class A{
    
     
	public static void main(String args){
    
     
		B b=new B();//语法正确 
	} 
}
class B{
    
    } 

package com.bao1; 
public class C{
    
     
	public static void main(String[] args){
    
     
		B b=new B1();//语法错误,因为B类是默认class 
	} 
}
//成员属性和成员方法:定义在class内部,除了构造器(匿名内部代码块)和析构器之外的其它内容 
public class A{
    
     
	public A(){
    
    } //方法名称和类名称一致,包括大小写,而且没有返回类型声明---构造器 
	//构造器方法不能直接调用,只能通过new运算符进行间接调用new A()系统自动执行A()方法 
	
	public int A(){
    
    } //语法正确,但是不是构造器。可以直接调用,new A().A() 
	
	//成员方法 
	public范围限定词 返回数据类型 方法名称(参数类型1 形参名称1,...){
    
    //如果没有返回值则 类型为void
		return xxx; 返回值必须和返回类型声明一致;如果返回类型为voidreturn后面不能 带任何数据 
	} //成员方法的调用为 对象名.方法名称(实际参数列表) 
}

abstract

The process of ignoring the details of an object or entity and focusing only on its essential characteristics can simplify functionality and format, and help users interact with objects

It is the basic magic weapon for human beings to solve problems. A good abstraction strategy can control the complexity of the problem and enhance the versatility and scalability of the system

Abstraction mainly includes process abstraction and data abstraction

  • Process abstraction is to extract operations with clear functional definitions in the problem domain and treat them as an entity
  • Data abstraction is a higher-level abstraction method than process abstraction. It binds the attributes and behaviors of the description object together to achieve a unified abstraction, so as to achieve a true simulation of real-world objects.

kind

Classes are templates and blueprints for constructing objects. In layman's terms, classes are equivalent to architectural drawings, and objects are equivalent to buildings. The process of constructing an object from a class is called creating an instance of the object. In Java, a "class" is defined by the keyword class, followed by the class name

The process of designing and writing classes is actually an abstract process of common attributes and behaviors of entities

For example, it is a specific individual: Zhao Xiaopang, in order to store the research object in the computer, a new data type Student is defined

class Student{
    
    }

Analyze the properties related to the problem domain:

class Student {
    
    
	private Long id;如果使用包装类,则默认值为null
	private String username=”yanjun”;
	private int age;如果使用简单类型,则数值型默认为0,布尔型为false
}

Analyzing Behavior Related to the Problem Domain - Methodology

class Student{
    
    
	public void study(){
    
    }
	public void sleep(){
    
    }
	
	private Long id;如果使用包装类,则默认值为null
	private String username=”yanjun”;
	private int age;
}

The purpose of defining a class is to store a specific object in the computer, so it is necessary to create the corresponding object, and the creation method is consistent with the meaning of the variable defined in the simple type

Student s1=new Student();
s1.方法名(); 调用成员方法

new Stduent().sleep();

Object-oriented programming can be thought of as class-oriented programming. The process of writing a class is equivalent to defining a new data type.
The core of object-object programming is abstract-oriented programming, and the core of abstract-oriented programming is interface-oriented programming

summary inheritance

Inheritance is to expand on the basis of existing classes to generate new classes, which supports the concept of hierarchical classification. Existing classes are called parent classes, super classes, or base classes, while newly created classes are called subclasses or derived classes. When the same attributes and behaviors exist in multiple classes, extract these contents into a single class, then multiple classes do not need to define these attributes and behaviors, as long as they inherit that class. Multiple classes can be called subclasses, and a single class is called a parent class or superclass.

  • Use the extends keyword to create an inheritance relationship between classes
    • Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits the methods from the parent class, so that the subclass has the same behavior as the parent class.
    • Inheritance is a technology that uses the definition of an existing class as the basis to create a new class. The definition of a new class can add new data or new functions, or use the functions of the parent class, but it cannot selectively inherit the parent class.
    • Inheritance is a mutual relationship of is-a
  • In Java, everything owned by the parent class can be inherited by the subclass
    • Can private properties be inherited - two answers
    • In addition to having the properties and methods of the parent class (with all the members of the parent class, but note that the constructor and destructor are not members
      ), the subclass can also create its own characteristics
public class Animal{
    
    
	private Long id;
	private String type;
	public void pp(){
    
    }
}
public class Person extends Animal{
    
    
	private String username;
	//这里就包含了方法pp
}
可以减少代码,而且易于维护
  • Suggested usage scenario: Put common operations and methods in the parent class, and define special methods in the subclass. The benefits are: 1. Avoid code duplication. 2. Avoid inconsistencies caused by human factors

Guess you like

Origin blog.csdn.net/qq_39756007/article/details/127233160