In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "simple class writing and inheritance, interface and other usage examples". The explanation in the article is simple and clear and easy to learn and understand. let's study and learn "simple class writing and inheritance, interface and other usage examples".
The writing of simple class and the usage of inheritance, interface, etc.
[post-naming reference coding demonstration]
[reference procedure]
Package cn.fldong.lesson_code;// definition parent class-human public class Person {/ / [1] definition attribute private String name; public String getName () {return name;} public void setName (String name) {this.name = name;} private int age Public int getAge () {return age;} public void setAge (int age) {this.age = age;} / / [2] defines the nonparametric and parameterized constructor public Person () {} public Person (String name, int age) {this.name = name This.age = age;} / / [3] the toString () method of re-object @ Override public String toString () {return "Person class [name=" + name + ", age=" + age + "]" } / / the common method defined by [4] work () void work () {System.out.println ("working methods implemented in the Person class!") ;} / / = / / [define Life Interface] public interface ILife {void eat (); void drink (); void play (); void happy ();} / / = / / define subclasses-Student public class Student extends Person implements ILife {/ / [1] defines attributes private String className; public String getClassName () {return className } public void setClassName (String className) {this.className=className;} / / [2] defines the construction method with and without parameters public Student () {} public Student (String name, int age,String className) {super (name, age); this.className=className } / / [3] override the toString () method @ Override public String toString () {return "Student class [name:=" + super.getName () + "age:=" + super.getAge () + "className:=" + className + "]" } / / [4] implement the interface method @ Override public void eat () {System.out.println ("students like fast food!") ; @ Override public void drink () {System.out.println ("students like Coke!") ; @ Override public void play () {System.out.println ("students like to play games!") ; @ Override public void happy () {System.out.println ("students love sleeping in class!") ;} / / the common method defined by study () public void study () {System.out.println ("Learning is the happiest thing in a student's life ~ _ ~");}} / / = / / define subclass teacher public class Teacher extends Person {/ / [1] define attribute private int workYears Public int getClassName () {return workYears;} public void setClassName (int workYears) {this.workYears = workYears;} / / [2] defines the construction method public Teacher () {} public Teacher (String name, int age,int workYears) {super (name, age) with and without parameters. This.workYears = workYears;} / / [3] override the toString () method @ Override public String toString () {return "Teacher class [workYears=" + workYears + "]" } / / [4] rewrite the parent method work () public void work () {System.out.println ("the teacher works very hard, please give me some face and support!") ;}} / = / / Test class-- the program entry public class MyMain {public static void main (String [] args) {/ / [1] instantiates a teacher object and calls the method to output the teacher's seniority Teacher objTeacher=new Teacher ("Dong Fuliang", 40Person20); objTeacher.work () Student objStudent=new Student ("Guan Xinmiao", 19, "Software Engineering Class 2"); objStudent.work (); System.out.println ("= the above two objects calling methods of the same name are not called polymorphisms because they have different types of Student and Teacher="); System.out.println () / / [2] Test Polymorphism calls work () method Person objT=new Teacher ("Mr. Dong", 30Jing 10); objT.work (); Person objS=new Student ("Zhang Yu", 20, "Software Engineering Class 2"); objS.work () System.out.println ("= two objects calling methods of the same name are called polymorphisms because they have the same type Person="); System.out.println () / / [3] when testing Person type variables (essentially Studeng class objects), call subclass unique (study ()) method Person obj=new Student ("Zhang Yu", 20, "Software Engineering Class 2"); / / obj.study () / / an error message will be displayed: The method study () is undefined for the type Person Student result= (Student) obj; result.study (); System.out.println ("= ="); System.out.println ("cannot be called until cast") System.out.println ("[Note] you must make sure that the instance of the subclass Student type assigned to the parent class Person variable can be cast!") Thank you for your reading. The above is the content of "simple class writing and inheritance, interface and other usage examples". After the study of this article, I believe you have a deeper understanding of the writing of simple classes and inheritance, interface and other usage examples, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.