Java中继承的需要是什么?
继承,正如我们都听说过的,是面向对象编程语言最重要的特性之一,无论是Java、C++ 还是任何其他 OOP 语言。但是继承的必要性是什么?为什么它是一个如此重要的概念
继承可以定义为一种机制,通过该机制一个对象可以获得父对象的所有属性(即数据成员)和行为(即成员函数或方法)。
继承的基本思想是从现有类(称为父类或基类或超类)创建新类(称为子类或派生类或子类)。即子类继承父类的属性(方法和字段)。
想在没有继承的情况下实现某事将是对时间和资源的滥用,因为我们必须一次又一次地编写相同的代码。
让我们举个例子,三个类 Person、Employee 和 Student 有一些属性,一些共同的,一些不同的。请参阅下面的代码。
Java
// Java program without inheritance
import java.io.*;
import java.math.BigDecimal;
// class person
class Person {
private String name;
private int 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; }
public String toString()
{
return String.format(
"Person :- name : %s , age : %d", name, age);
}
}
//class student
class Student {
private String name;
private int age;
private int rollno;
private String school;
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 int getRollno() { return rollno; }
public void setRollno(int rollno)
{
this.rollno = rollno;
}
public String getSchool() { return school; }
public void setSchool(String school)
{
this.school = school;
}
public String toString()
{
return String.format(
"Student :- name : %s , age : %d , roll : %d , school : %s",
name, age, rollno, school);
}
}
//class Employee
class Employee {
private String name;
private int age;
private double salary;
private String organisation;
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 double getSalary() { return salary; }
public void setSalary(double salary)
{
this.salary = salary;
}
public String getOrganisation() { return organisation; }
public void setOrganisation(String organisation)
{
this.organisation = organisation;
}
public String toString()
{
return String.format(
"Employee :- name : %s , age : %d , organisation : %s , salary : %f",
name, age, organisation, salary);
}
}
// class PersonRunner
public class PersonRunner {
public static void main(String[] args)
{
Person person = new Person();
Student student = new Student();
Employee employee = new Employee();
person.setName("Saurabh");
person.setAge(20);
student.setName("Prateek");
student.setAge(21);
student.setRollno(101);
student.setSchool("New Era HS");
employee.setName("Sushant");
employee.setAge(25);
employee.setOrganisation("GeeksforGeeks");
employee.setSalary(50000.00);
System.out.println(person);
System.out.println(student);
System.out.println(employee);
}
}
Java
// Java program with inheritance
import java.io.*;
class Person {
private String name;
private int 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; }
public String toString()
{
return String.format(
"Person :- name : %s , age : %d", getName(),
getAge());
}
}
// Student class is inheriting the properties of Person
// class( through extend keyword) Therefore, we don't need to
// declare name and age (and their related methods which are
// covered in Person) again.
class Student extends Person {
private int rollno;
private String school;
public int getRollno() { return rollno; }
public void setRollno(int rollno)
{
this.rollno = rollno;
}
public String getSchool() { return school; }
public void setSchool(String school)
{
this.school = school;
}
public String toString()
{
return String.format(
"Student :- name : %s , age : %d , roll : %d , school : %s",
getName(), getAge(), getRollno(), getSchool());
}
}
// Employee class is inheriting the properties of Person
// class( through extend keyword) Therefore, we don't need to
// declare name and age (and their related methods which are
// covered in Person) again.
class Employee extends Person {
private double salary;
private String organisation;
public double getSalary() { return salary; }
public void setSalary(double salary)
{
this.salary = salary;
}
public String getOrganisation() { return organisation; }
public void setOrganisation(String organisation)
{
this.organisation = organisation;
}
public String toString()
{
return String.format(
"Employee :- name : %s , age : %d , organisation : %s , salary : %f",
getName(), getAge(), getOrganisation(),
getSalary());
}
}
public class PersonRunner {
public static void main(String[] args)
{
Person person = new Person();
Student student = new Student();
Employee employee = new Employee();
person.setName("Saurabh");
person.setAge(20);
student.setName("Prateek");
student.setAge(21);
student.setRollno(101);
student.setSchool("New Era HS");
employee.setName("Sushant");
employee.setAge(25);
employee.setOrganisation("GeeksforGeeks");
employee.setSalary(50000.00);
System.out.println(person);
System.out.println(student);
System.out.println(employee);
}
}
Person :- name : Saurabh , age : 20
Student :- name : Prateek , age : 21 , roll : 101 , school : New Era HS
Employee :- name : Sushant , age : 25 , organisation : GeeksforGeeks , salary : 50000.000000
请注意,我们必须在 Student 和 Employee 类中再次编写已经在 Person 类中编写的相同代码。这需要时间和记忆。
现在仔细阅读下面我们使用继承的代码。
Java
// Java program with inheritance
import java.io.*;
class Person {
private String name;
private int 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; }
public String toString()
{
return String.format(
"Person :- name : %s , age : %d", getName(),
getAge());
}
}
// Student class is inheriting the properties of Person
// class( through extend keyword) Therefore, we don't need to
// declare name and age (and their related methods which are
// covered in Person) again.
class Student extends Person {
private int rollno;
private String school;
public int getRollno() { return rollno; }
public void setRollno(int rollno)
{
this.rollno = rollno;
}
public String getSchool() { return school; }
public void setSchool(String school)
{
this.school = school;
}
public String toString()
{
return String.format(
"Student :- name : %s , age : %d , roll : %d , school : %s",
getName(), getAge(), getRollno(), getSchool());
}
}
// Employee class is inheriting the properties of Person
// class( through extend keyword) Therefore, we don't need to
// declare name and age (and their related methods which are
// covered in Person) again.
class Employee extends Person {
private double salary;
private String organisation;
public double getSalary() { return salary; }
public void setSalary(double salary)
{
this.salary = salary;
}
public String getOrganisation() { return organisation; }
public void setOrganisation(String organisation)
{
this.organisation = organisation;
}
public String toString()
{
return String.format(
"Employee :- name : %s , age : %d , organisation : %s , salary : %f",
getName(), getAge(), getOrganisation(),
getSalary());
}
}
public class PersonRunner {
public static void main(String[] args)
{
Person person = new Person();
Student student = new Student();
Employee employee = new Employee();
person.setName("Saurabh");
person.setAge(20);
student.setName("Prateek");
student.setAge(21);
student.setRollno(101);
student.setSchool("New Era HS");
employee.setName("Sushant");
employee.setAge(25);
employee.setOrganisation("GeeksforGeeks");
employee.setSalary(50000.00);
System.out.println(person);
System.out.println(student);
System.out.println(employee);
}
}
Person :- name : Saurabh , age : 20
Student :- name : Prateek , age : 21 , roll : 101 , school : New Era HS
Employee :- name : Sushant , age : 25 , organisation : GeeksforGeeks , salary : 50000.000000
请注意,与第一个未使用继承的示例相比,我们在上面的示例中使用继承时编写的代码较少,因此我们必须一次又一次地定义名称、年龄及其相关方法。
也就是说,我们利用了可重用性的概念,因为我们一遍又一遍地重用了在 Person 类中编写的代码。
另外,上面的例子展示了Method Overriding(因为Person类的toString函数在Student和Employee类中都被覆盖了,这也是继承的一大特点。
因此,它得出结论,继承的主要目的是实现可重用性的概念,节省我们的时间和资源,并在不同的类之间建立更好的连接,并实现方法覆盖。