Home  >  Article  >  Java  >  What are the three major characteristics of java

What are the three major characteristics of java

青灯夜游
青灯夜游Original
2023-01-06 14:30:2526695browse

The three major characteristics of Java are: 1. Encapsulation, which is to hide some information of a class inside the class and not allow direct access by external programs. Instead, the hidden information is realized through the methods provided by the class. Operation and access. 2. Inheritance means that the subclass has all the properties and methods of the parent class, thereby realizing code reuse. 3. Polymorphism means that parent class references point to subclass objects, thus producing multiple forms.

What are the three major characteristics of java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

The three major characteristics of Java

The three major characteristics of object-oriented: encapsulation, inheritance, and polymorphism.

Encapsulation:

  • Hides some information of the class inside the class and does not allow direct access by external programs. Instead, the hidden information is operated through the methods provided by the class. and access.

Inheritance:

  • The subclass owns all the properties and methods of the parent class (except for privately modified properties), thus realizing the reuse of implementation code;

Polymorphism:

  • Use the parent class reference to accept object instances of different subclasses. The parent class reference calls the same method, and generates different instances according to the subclass. Different results

1, encapsulation

1) What is encapsulation

Concept: Hide the internal implementation details of the object as much as possible, and control the modification and access permissions of the object.

Access modifier: private (attributes can be modified as private, visible only to this class)

2) Public access method

In the form of access methods, assignment and value acquisition operations are completed.

Problem: Illegal data entry is still not solved!

  • Provide public access methods to ensure normal data entry.
  • Naming specification:
  • Assignment: setXXX() //Use method parameters to achieve value assignment
  • Value: getXXX() //Use method return value to achieve value

3) Example

public static void main(String[] args) {
		int a;
		Num num = new Num();
//		传入值100
		num.setNum(100);
		System.out.println(num.getNum());
	}
private int a;

//	在调用get方法时,返回本类a的值
	public int getNum() {
		return a;
	}

//	接受传入的值100,并赋值给本类的a
	public void setNum(int num) {
		this.a = num;
	}

4) Filter valid data

In the public access method, add logical judgment to filter out illegal data to ensure data security.

5) Summary

The get/set method is the only channel for the outside world to access the private properties of the object. The data can be detected and filtered inside the method. .

2. Inheritance

1) Inheritance in the program

  • Inheritance in a program is a gift or acquisition of characteristics and behaviors between classes.
  • The inheritance relationship between two classes must satisfy the "is a" relationship.

2) Selection of parent category

  • In real life, there are differences between many categories Inheritance relationships all satisfy the "is a" relationship.

  • A dog is an animal, a dog is a living thing, and a dog is a substance.

  • Multiple categories can be used as the parent category of "dog", and the most suitable parent category needs to be selected.

  • The more refined the function, the more overlapping points, and the closer it is to the direct parent class.

  • The rougher the function, the fewer overlapping points, and the closer it is to the Object class. (The concept that everything is an object)

3) Inheritance

Syntax: class subclass extends parent Class { } //When defining a subclass, display the inherited parent class

public class 子类名 extends 父类名{
	
	//代码块
	}

Application: After the inheritance relationship is generated, the subclass can use the attributes and methods in the parent class, or define unique properties for the subclass properties and methods.

Benefits: It not only improves the reusability of the code, but also improves the scalability of the code.

4) Characteristics of inheritance

Java is single inheritance. A class can only have one direct parent class, but it can have multi-level inheritance. Properties and methods stack up one level at a time.

5) Not inheritable

Constructor method: The constructor method in a class is only responsible for creating objects of this class and cannot be inherited.

Private modified properties and methods: a type of access modifier, visible only to this class.

When the parent and child classes are not in the same package, the attributes and methods modified by default: a type of access modifier, only visible in the same package.

3. Method rewriting

1) Method rewriting /Override

Method rewriting principle:

  • The method name and parameter list are the same as those of the parent class.
  • The return value type must be the same as the parent class or its subclass
  • The access modifier can be the same as the parent class or wider than the parent class.

Execution of method rewriting:

  • After a subclass overrides a parent class method, the rewritten method of the subclass will be executed first when called.

  • Characteristics of method overriding:

    When a subclass overrides a parent class method, the subclass method will override the parent class method.

    Subclasses override parent class methods, and the access level cannot be stricter than parent class methods.

    The subclass override method name and type are the same as the parent class.

    父类的构造方法无法重写,只能被覆盖。

示例:

//父类
public class Animal {
//	父类中吃的方法会输出“玩玩玩”
	public void play() {
		System.out.println("玩玩玩");
	}

//	父类中睡的方法会输出“睡睡睡”
	public void sleep() {
		System.out.println("睡睡睡");
	}

}
/**
 * 狗类继承 父类
 */
public class Dog extends Animal {

//	进行方法重写,将方法重写输出为“狗玩飞碟”
	public void play() {
		System.out.println("狗玩飞碟");
	}
}
public class Test {

	public static void main(String[] args) {
		// 实例化宠物对象
		Dog d = new Dog();
		d.play();
		d.sleep();
	}

}

运行输出:

What are the three major characteristics of java

2)方法重写与方法重载的区别

相同点:方法名相同
不同点:
重载:参数列表不同,返回值与访问修饰符无关
重写:参数列表相同,返回值相同或其子类,访问修饰符不能比父类更严

4、super关键字

1)super关键字

super关键字可在子类中访问父类的方法。

  • 使用”super.”的形式访问父类的方法,进而完成在子类中的复用;
  • 再叠加额外的功能代码,组成新的功能。

2)super调用父类无参构造

super():表示调用父类无参构造方法。如果没有显示书写,隐式存在于子类构造方法的首行。

3)super调用父类有参构造

super():表示调用父类无参构造方法。

super(实参):表示调用父类有参构造方法。
参构造被执行

4)this与super

this或super使用在构造方法中时,都要求在首行。
当子类构造中使用了this()或this(实参),即不可再同时书写super()或super(实参),会由this()指向构造方法完成super()调用。

class A{
		public A(){
		System.out.println(( "A-无参构造"));
		}
		public A(int value) {
		System.out.println(("A-有参构造")); 
		}
		}
		class B extends A{
		public B(){
		super();
		System.out.println( "B-无参构造");
		}
		public B(int value) {
//		super();这两货不能跟同时存在
		this();
		System.out.println(("B-有参构造"));
		}
		}

5、多态

概念:父类引用指向子类对象,从而产生多种形态。

二者具有直接或间接的继承关系时,父类引用可指向子类对象,即形成多态。

父类引用仅可调用父类所声明的属性和方法,不可调用子类独有的属性和方法。

1)多态的应用

方法重载可以解决接收不同对象参数的问题,但其缺点也比较明显。

  • 首先,随着子类的增加,Master类需要继续提供大量的方法重载,多次修改并重新编译源文件。
  • 其次,每一个feed方法与某一种具体类型形成了密不可分的关系,耦合太高。

场景一:使用父类作为方法形参实现多态,使方法参数的类型更为宽泛。

public class Animal {
//		父类中吃的方法会输出“玩玩玩”
	public void play() {
		System.out.println("玩玩玩");
	}

//	父类中睡的方法会输出“睡睡睡”
	public void sleep() {
		System.out.println("睡睡睡");
	}

}
/**
 * 狗类继承 父类
 * 
 * 
 *
 */
public class Dog extends Animal {

//	狗类特有的方法“狗吃狗粮”
	public void eat() {
		System.out.println("狗吃狗粮");
	}

}
public class Test {

	public static void main(String[] args) {
		// 实例化宠物对象
		Animal d = new Dog();
		d.play();
		d.sleep();
//		The method eat() is undefined for the type Animal
//		对于类型动物,eat()方法未定义
//		当我们去调用子类对象的特有方法时,就会爆出上面的错误提示
//		如果想要实现子类特有方法,就必须要强转
//		d.eat();
		((Dog) d).eat();
	}

}

运行输出:

What are the three major characteristics of java

场景二:使用父类作为方法返回值实现多态,使方法可以返回不同子类对象。

示例:

//动物类  父类
public class Animal {
	public void food() {
		System.out.println("...");
	}
}
//用extends关键字,继承父类属性
public class Dog extends Animal {

	public void food() {
		System.out.println("狗吃狗粮");
	}

	public void runing() {
		System.out.println("一直跑跑跳跳");
	}
}
//用extends关键字,继承父类属性
public class Fish extends Animal {
	public void food() {
		System.out.println("大鱼吃小鱼,小鱼吃虾米");
	}

	public void swimming() {
		System.out.println("小鱼儿,一直游");
	}
}
public class Master {
//	传入你的动物,并去给它喂食
	public void food(Animal animal) {
		System.out.println("喂食");
		animal.food();
	}
}
import java.util.Scanner;

public class Shopping {
//	你没有动物,所以animal为空
	Animal animal = null;

//	判断你要购买的宠物,并返回宠物类(狗、鱼)
	public Animal shopping(int a) {
		if (a == 1) {
			animal = new Dog();
		} else if (a == 2) {
			animal = new Fish();
		}
//		this.animal=animal;
		return animal;
	}

	public void showMenu() {
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎来到一只宠物宠物店");
		System.out.println("请选择喜欢的宠物:");
		System.out.println("1.狗 2.鱼 ");
		int a = input.nextInt();
		Animal animal = shopping(a);
		Master mm = new Master();
		mm.food(animal);
//		用instanceof判断你买的是狗还是鱼。
//		狗就执行狗的属性和方法,鱼就执行鱼的属性和方法
		if (animal instanceof Dog) {
			Dog d = (Dog) animal;
			d.runing();
		} else if (animal instanceof Fish) {
			Fish f = (Fish) animal;
			f.swimming();
		}

	}
}
//测试类
public class text {
	public static void main(String[] args) {
		Shopping shop = new Shopping();
		shop.showMenu();
	}
}

运行结果:

What are the three major characteristics of java

2)多态的静态和动态实现

动态绑定:即为重写/覆盖,方法的重写

动态绑定也叫后期绑定,在运行时,虚拟机根据具体对象实例的类型进行绑定,或者说是只有对象在虚拟机中运行创建了之后,才能确定方法属于哪一个对象实例的

  • 根据实际对象是什么,就去找相应对象方法去执行。
  • 动态绑定是在运行时才会执行(例如重写的方法)。

静态绑定:即为重载,方法的重载

一个方法的参数在编译阶段常被静态地绑定,它是根据参数列表的不同来区分不同的函数,通过编辑之后会变成两个不同的函数

  • 根据类型找相应的属性或者静态变量。
  • 静态绑定是在编译时执行(如成员变量,静态方法)。

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of What are the three major characteristics of java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn