Object-oriented basics 10

Source: Internet
Author: User

 

Object polymorphism: Development Suggestion: do not inherit an implemented class ------- interface and abstract class person {public void say () {system. out. println (this. getcontent ();} public abstract string getcontent ();} class worker extends person {Public String getcontent () {return "the worker spoke... ";}} Class student extends person {Public String getcontent () {return" students spoke... ";}} Public class javaoo05 {public static void main (string [] ARGs) {person P = new student (); p. say () ;}} if you need to inherit in future development, in 80% cases, all the inherited classes will be an abstract class. Of course, there is also a problem that abstract classes have restrictions on Java single inheritance, abstract class person {private string name; private int age; Public Person (string name, int age) {This. setname (name); this. setage (AGE);} public void setname (string name) {This. name = Name;} public void setage (INT age) {This. age = Age;} Public String getname () {return this. name;} public int getage () {return this. age;} public void say () {system. out. println (this. getcontent ();} public abstract string getcontent ();} class worker extends person {private float salary; public worker (string name, int age, float salary) {super (name, age); this. setsalary (salary);} public void setsalary (float salary) {This. salary = salary;} Public float getsalary () {return this. salary;} Public String getcontent () {return "worker says ---- name:" + super. getname () + ", age:" + getage () + ", salary:" + this. getsalary () ;}} class student extends person {private float score; Public student (string name, int age, float score) {super (name, age); this. setscore (score);} public void setscore (float score) {This. score = score;} public float getscore () {r Eturn this. score;} Public String getcontent () {return "Student description ---- name:" + super. getname () + ", age:" + super. getage () + ", score:" + this. getscore () ;}} public class javaoo05 {public static void main (string [] ARGs) {person P = NULL; P = new student ("James ); // P = new worker ("zhangsan",); p. say () ;}} interface a {public void fun ();} Class B implements a {public void fun () {system. out. println ("Hello World !!! ") ;}} Public class javaoo05 {public static void main (string [] ARGs) {A = new B ();. fun ();}} ######################################## ####################### interface USB {public void start (); public void stop ();} class PC {public static void plugin (usb u) {u. start (); U. stop () ;}} class MP3 implements USB {public void start () {system. out. println ("MP3 started working... ");} Public void stop () {system. Out. println (" MP3 stops working... ") ;}} Class udisk implements USB {public void start () {system. Out. println (" U disk started to work... ");} Public void stop () {system. Out. println (" U disk stops working... ") ;}} Public class javaoo05 {public static void main (string [] ARGs) {PC. plugin (New udisk (); PC. plugin (new MP3 () ;}} the USB flash drive is starting to work... U disk stops working... MP3 started to work... MP3 stops working... Interface A {public void fun1 (); Public void fun2 (); Public void fun3 ();} abstract class B implements a {public void fun1 () {} public void fun2 () {} public void fun3 () {}} Class C extends B {public void fun1 () {system. out. println ("Hello world !! ") ;}} Public class javaoo05 {public static void main (string [] ARGs) {A = new C (); //. fun2 ();. use of the fun1 () ;}} interface: expose the method name to the instance of the user interface object: the actual application of the interface instantiated by the sub-class as the interface object through the object polymorphism: USB (Interface) ---- start work after insertion, and stop exiting PC after work ------ à USB <------- mp3.udisk abstract class is similar to the interface in use, so who should I choose? Development: The interface is preferred. The interface allows multiple inheritance interfaces: USB {public void start (); Public void stop ();} class MP3 implements USB {public void start () {system. out. println ("MP3 started working... ");} Public void stop () {system. Out. println (" MP3 stops working... ") ;}} Class udisk implements USB {public void start () {system. Out. println (" U disk started to work... ");} Public void stop () {system. Out. println (" U disk is finished... ") ;}} Class factory {public static USB getusbinstance () {// return new MP3 (); return New udisk ();}} public class javaoo05 {public static void main (string [] ARGs) {USB u = factory. getusbinstance (); U. start (); U. stop () ;}} interface and abstract class:

  Abstract class Interface
Same Objects cannot be instantiated directly. Through polymorphism, objects can be instantiated by their subclasses.
Different Including general methods, abstract methods, variables, constants Including constants and abstract methods
Constructor Cannot have Constructor
Abstract classes can implement multiple interfaces The interface cannot inherit an abstract class.
Individual inheritance has limitations. Solve the limitations of single inheritance
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.