Java and c # are both object-oriented languages.
Object-oriented languages have three main features: encapsulation, inheritance, and polymorphism.
Encapsulation
Encapsulation: hides the attributes and implementation details of an object and only exposes the interface to control the read and modify access levels of attributes in the program.
Class Person2 {private String name; public void setName (String name) {this. name = name;} public String getName () {return name;} private String sex; public void setSex (String sex) {this. sex = sex;} public String getSex () {return sex;} private int age; public void setAge (int age) {this. age = age;} public int getAge () {return age;} private double height; public void setHeight (double height) {this. height = height;} public double getHeight () {return height;} public class Person1 {public static void main (String arges []) {Person2 p2 = new Person2 (); p2.setName ("yellow"); p2.setSex ("male"); p2.setAge (20); p2.setHeight (1.70); System. out. println ("name:" + p2.getName () + ", Gender:" + p2.getSex () + ", age:" + p2.getAge () + ", height: "+ p2.getHeight ());}}
Inheritance
Inheritance: It occurs between classes and inherits from existing classes. It shares the members and methods of the parent class through inheritance and adds new methods. It can be a single inheritance or multiple inheritance.
In Java, the inherited class is called superclass, And the inherited class is called subclass ). Therefore, a subclass is a function extension of a superclass. It inherits all attributes and methods defined by the superclass and adds special functional methods.
Class Father {public String name; public int age; public String eye; public String height; public String cutis; // Father class constructor public Father () {} public Father (String name, int age) {this. name = name; this. age = age;} // rewrite the toString method in the Object class/* public String toString () {String emp = "name:" + name + ", age:" + age; return emp;} */} public class son extends Father {public String spectacle_frame; public son () {} public son (String spectacle_frame) {this. spectacle_frame = spectacle_frame;} public void getMes () {System. out. println (spectacle_frame);} public static void main (String arges []) {Father f = new Father ("Father", 50); System. out. println (f); son s = new son ("Blue"); s. name = "Tom"; s. age = 20; System. out. println (s); s. getMes ();}}
Note: rewrite the toString method in the Object class.
Uncomment: override the toString method in the Object class.
Polymorphism
Polymorphism: multiple implementation methods of interfaces are polymorphism, and the same object has multiple forms at the same time.
Class fruit {public void getMes () {System. out. println ("parent class") ;}} class apple extends fruit {public void getMes () {System. out. println ("apple subclass");} class orange extends fruit {public void getMes () {System. out. println ("orange subclass") ;}} public class racing_cycel {public void show (fruit f) {f. getMes ();} public static void main (String arges []) {racing_cycel rc = new racing_cycel (); fruit f = new fruit (); apple a = new apple (); orange o = new orange (); rc. show (f); rc. show (a); rc. show (o );}}
Running result