Java programming ideology Reading Notes-2 (chapter 2)

Source: Internet
Author: User

Java programming ideology Reading Notes 2nd ~ Chapter 7


Chapter 2 interfaces and inner and hidden classes
I. Interface
1. If the class implementing the interface does not implement all functions in the interface, the class must be declared as abstract class, And the unimplemented functions in the interface are abstract class in this class.

  1. InterfaceInterface {
  2. Public VoidF ();
  3. Public VoidG ();
  4. }
  5. Abstract ClassFirstImplementsInterface {
  6. Public VoidF (){}
  7. }
  8. ClassSecondExtendsFirst {
  9. Public VoidG (){}
  10. }
  11. Public ClassExplicitStatic {
  12. Public Static VoidMain (String[] Args ){
  13. Interface f =NewSecond ();
  14. F. f ();
  15. F. g ();
  16. }
  17. }

2. All functions in the interface automatically have the public access permission. Therefore, when implementing an interface, all functions inherited from this interface must be defined as public.
  1. InterfaceMyInterface {
  2. Public VoidF ();
  3. VoidG ();
  4. }
  5. ClassFirstImplementsMyInterface {
  6. Public VoidF (){}
  7. //! Void g () {} error, should be defined as public
  8. }

3. Data members in the interface automatically become static and final
  1. InterfaceMyInterface {
  2. IntI = 5;
  3. VoidF ();
  4. VoidG ();
  5. }
  6. ClassFirstImplementsMyInterface {
  7. Public VoidF (){}
  8. Public VoidG (){}
  9. }
  10. Public ClassExplicitStatic {
  11. Public Static VoidMain (String[] Args ){
  12. MyInterface x =NewFirst ();
  13. // The data member I of MyInterface is static and can be called directly
  14. System. Out. println ("MyInterface. I =" + MyInterface. I + ", x. I =" + x. I );
  15. // The data member I of MyInterface is final and cannot be modified
  16. // X. I ++;
  17. // MyInterface. I ++;
  18. }
  19. }

4. Multi-Inheritance
1) devriced class can inherit multiple interfaces and one abstract or concrete base class at the same time. If both base class and interface are inherited, you must first write down the name of the concrete class, and then the name of interfaces.
2) If the representative class inherited by derived class has the same function as interfaces, you can not implement that function in derived class.
  1. InterfaceCanFight {
  2. VoidFight ();
  3. }
  4. InterfaceCanSwim {
  5. VoidSwim ();
  6. }
  7. ClassActionCharacter {
  8. Public VoidFight (){}
  9. }
  10. ClassHeroExtendsActionCharacter
  11. ImplementsCanFight, CanSwim {
  12. Public VoidSwim (){};
  13. }
  14. Public ClassExplicitStatic {
  15. Static VoidF (CanFight x) {x. fight ();}
  16. Static VoidS (CanSwim x) {x. swim ();}
  17. Static VoidA (ActionCharacter x) {x. fight ();}
  18. Static VoidH (Hero x ){
  19. X. fight (); x. swim ();
  20. }
  21. Public Static VoidMain (String[] Args ){
  22. Hero h =NewHero ();
  23. F (h); s (h); a (h); h (h );
  24. }
  25. }

Because in Actio

Related Article

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.