Java programming ideology version 4 * Chapter 7 * personal exercises and programming ideology version 4

Source: Internet
Author: User
Tags class manager export class

Java programming ideology version 4 * Chapter 7 * personal exercises and programming ideology version 4
Welcome to group: 239063848
Notice on entering the Group: This group is only used for technology sharing and communication, issue and answer
No chat, no advertisement, no recruitment ...... If You Are the One

Exercise 1: (2) create a simple class. In the second class, a reference is defined as the object of the first class. Instantiate this object using inertia initialization.

Package test; public class Manager {public static void main (String args []) {Second s = new Second (); s. getFirst ();}/*** print the result: */} class First {} class Second {First f; Second () {System. out. println ("Creating Second");} First lazy () {if (f = null) {System. out. println ("Creating First"); f = new First ();} return f;} public First getFirst () {return lazy ();}}

Exercise 2: (2) inherit from Detergent to generate a new class. Override scrub () and add a new method named sterilize.

Package test; public class Manager {public static void main (String args []) {Sub s = new Sub (); s. apply (); s. dilute (); s. foam (); s. scrub (); s. sterilize (); new print (s);}/*** print the result: Cleanser apply () dilute () foam () sub. scrub Detergent. scrub () sub. sterilize () */} class print {print (Object obj) {System. out. println (obj) ;}} class Cleanser {private String s = "Cleanser"; public void append (String a) {s + = a;} public void dilute () {append ("dilute ()") ;}public void apply () {append ("apply ()") ;}public void scrub () {append ("scrub () ");} public String toString () {return s;} public static void main (String [] args) {Cleanser x = new Cleanser (); x. dilute (); x. apply (); x. scrub (); new print (x) ;}} class Detergent extends Cleanser {public void scrub () {append ("Detergent. scrub () ");} public void foam () {append (" foam () ");} class Sub extends Detergent {public void scrub () {append (" sub. scrub "); super. scrub ();} public void sterilize () {append ("sub. sterilize ()");}}
Exercise 3 (2) prove the previous two sentences (even if you do not create a constructor for Cartoon, the compiler will synthesize a default constructor for you, the constructor will call the constructor of the base class)

Package test; public class Manager {public static void main (String args []) {new Cartoon ();}/*** print the result: art constructor Drawing constructor */} class print {print (Object obj) {System. out. println (obj) ;}} class Art {Art () {new print ("Art constructor") ;}} class Drawing extends Art {Drawing () {new print ("Drawing constructor") ;}} class Cartoon extends Drawing {}

Exercise 4 (2) proves that the base class constructor is always called and called before exporting the class constructor.

Package test; public class Manager {public static void main (String args []) {new Child ();}/*** print the result: parent class constructor outputs subclass constructor outputs */} class print {print (Object obj) {System. out. println (obj) ;}} class Parent {Parent () {new print ("base class constructor output") ;}} class Child extends Parent {Child () {new print ("subclass constructor output ");}}

Exercise 5: (1) create two classes A and Class B with the default constructor (empty parameter list. Inherit from A to generate A new member named C and create A member of Class B in C. Do not write constructor for C. Create a class C object and observe its results.
Package test; public class Manager {public static void main (String args []) {new C ();}/*** print the result: ()...... B ()...... B ()...... C ()...... */} Class print {print (Object obj) {System. out. println (obj) ;}} class A {A () {new print ("()...... ") ;}} Class B {B () {new print (" B ()...... ") ;}} Class C extends A {private B B = new B (); C () {new print (" C ()...... ");} Private B b2 = new B ();}

Exercise 6: (1) Use Chess to prove the first two words

Package test; public class Manager {public static void main (String args []) {new Chess ();}/*** print the result: Exception in thread "main" java. lang. error: Unresolved compilation problem: Implicit super constructor BordGame () is undefined. must explicitly invoke another constructorat test. chess. <init> (Manager. java: 32) at test. manager. main (Manager. java: 6) */} class print {print (Object obj) {System. out. println (obj) ;}} class Game {Game (int I) {new print ("Game constructor") ;}} class BordGame extends Game {BordGame (int I) {super (I); new print ("BordGame constructor") ;}} class Chess extends BordGame {Chess () {// Implicit super constructor BordGame () is undefined. must explicitly invoke another constructornew print ("Chess constructor ");}}

Exercise 7: (1) Modify exercise 5 so that A and B Replace the default constructor with A constructor with parameters. Write a constructor for C and execute all initialization in it.

Package test; public class Manager {public static void main (String args []) {new C ();}/*** print the result: ()...... B ()...... B ()...... C ()...... */} Class print {print (Object obj) {System. out. println (obj) ;}} class A {A (int I) {new print ("()...... ") ;}} Class B {B () {new print (" B ()...... ") ;}} Class C extends A {private B B = new B (); C () {super (1); // new print (" C ()...... ");} Private B b2 = new B ();}

Exercise 8: (1) create a base class with only one non-default constructor, and create an export class with the default constructor and non-default constructor. Call the constructors of the base class in the constructor of the export class.
Package test; public class Test {public static void main (String [] args) {}} class A {/** non-default constructor **/A (int I) {System. out. println ("base class") ;}} class B extends A {B () {super (1 ); /** call the base class constructor **/} B (int I) {super (I);/** call the base class constructor **/System. out. println ("Export class ");}}


Exercise 9: (2) create a Root class that contains an instance of each class named Component1, Component 2, and Component3 (which is also written by you ). A Stem class derived from Root also contains the above "components ". All classes should have a default constructor that can print information about the class.

Package test; public class Test {public static void main (String [] args) {new Stem ();} /*** output Component1 component constructorRoot component constructorComponent3 constructorStem constructor */} class Root {private Component1 component1 = new Component1 (); private Component2 component2 = new Component2 (); private Component3 component3 = new Component3 (); Root () {System. out. println ("Root constructor") ;}} class Stem extends Root {private Component1 component1 = new Component1 (); private Component2 component2 = new Component2 (); private Component3 component3 = new Component3 (); Stem () {System. out. println ("Stem constructor") ;}} class Component1 {Component1 () {System. out. println ("Component1 constructor") ;}} class Component2 {Component2 () {System. out. println ("Component2 constructor") ;}} class Component3 {Component3 () {System. out. println ("Component3 constructor ");}}


Exercise 10: (1) Modify exercise 9 so that each class has only a non-default constructor.

Package test; public class Test {public static void main (String [] args) {new Stem (1 );} /*** output Component1 constructor 1Component2 constructor extends constructor 3 Root extends constructor 1Component2 constructor extends limit 3 Stem constructor */} class Root {private Component1 component1 = new Component1 (1 ); private Component2 component2 = new Component2 (2); private Component3 component3 = new Component3 (3); Root (int I) {System. out. println ("Root constructor") ;}} class Stem extends Root {private Component1 component1 = new Component1 (1); private Component2 component2 = new Component2 (2 ); private Component3 component3 = new Component3 (3); Stem (int I) {super (I); System. out. println ("Stem constructor") ;}} class Component1 {Component1 (int I) {System. out. println ("Component1 constructor" + I) ;}} class Component2 {Component2 (int I) {System. out. println ("Component2 constructor" + I) ;}} class Component3 {Component3 (int I) {System. out. println ("Component3 constructor" + I );}}

Exercise 11: (3) Modify Detergent. java. Use a proxy.

Package test; public class Test {public static void main (String args []) {Sub s = new Sub (); s. apply (); s. dilute (); s. foam (); s. scrub (); s. sterilize (); new print (s);}/*** print the result: Cleanser apply () dilute () foam () sub. scrub Detergent. scrub () sub. sterilize () */} class print {print (Object obj) {System. out. println (obj) ;}} class Cleanser {private String s = "Cleanser"; public void append (String a) {s + = a;} public void dilute () {append ("dilute ()") ;}public void apply () {append ("apply ()") ;}public void scrub () {append ("scrub () ");} public String toString () {return s;} public static void main (String [] args) {Cleanser x = new Cleanser (); x. dilute (); x. apply (); x. scrub (); new print (x) ;}} class Detergent {Cleanser = new Cleanser (); public void append (String str) {Cleanser. append (str);} public void dilute () {append ("dilute ()");} public void apply () {append ("apply ()");} public String toString () {return Cleanser. toString ();} public void scrub () {append ("Detergent. scrub () ");} public void foam () {append (" foam () ");} class Sub extends Detergent {public void scrub () {append (" sub. scrub "); super. scrub ();} public void sterilize () {append ("sub. sterilize ()");}}

Exercise 12: (3) Add the hierarchy of an appropriate dispose () method to all classes in exercise 9.

Package test; public class Test {public static void main (String [] args) {Stem s = new Stem (); try {s. toString ();} finally {s. dispose () ;}}/*** output Component1 constructorComponent2 constructorComponent3 constructorRoot implements multiple constructorStem disposeRoot dispose */} class Root {private Component1 component1 = new Component1 (); private Component2 component2 = new Component2 (); private Component3 component3 = new Component3 (); Root () {System. out. println ("Root constructor");} void dispose () {System. out. println ("Root dispose") ;}} class Stem extends Root {private Component1 component1 = new Component1 (); private Component2 component2 = new Component2 (); private Component3 component3 = new Component3 (); Stem () {System. out. println ("Stem constructor");} void dispose () {System. out. println ("Stem dispose"); super. dispose () ;}} class Component1 {Component1 () {System. out. println ("Component1 constructor");} void dispose () {System. out. println ("Component1 dispose") ;}} class Component2 {Component2 () {System. out. println ("Component2 constructor");} void dispose () {System. out. println ("Component2 dispose") ;}} class Component3 {Component3 () {System. out. println ("Component3 constructor");} void dispose () {System. out. println ("Component3 dispose ");}}

Exercise 13: (2) create a class with a method that is overloaded three times. Inherit to generate a new class and add a new overload definition for this method. This shows that these four methods can be used in the export class.


Package test; public class Test {public static void main (String [] args) {Stem s = new Stem (); s. a (); int I = 10; s. a (I); float f = 10f; s. a (f); double d = 10d; s. a (d);}/*** Output a () a (int) 10a (float) 10.0a (double) 10.0 */} class Root {void a () {System. out. println ("a ()");} void a (int I) {System. out. println ("a (int)" + I);} void a (float I) {System. out. println ("a (float)" + I) ;}} class Stem extends Root {void a (double I) {System. out. println ("a (double)" + I );}}

Exercise 14: (1) Add a service () to the Engine in Car. java and call this method in main.

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.