An object-oriented anonymous inner class question in the first season of Java entry

Source: Internet
Author: User

Interview Question one:

/*
According to the requirements, the code
Interface Inter {void Show ();
Class Outer {//Padded code}
Class Outerdemo {
public static void Main (string[] args) {
Outer.method (). Show ();
}
}
Required to output "HelloWorld" on the console
*/

Interface Inter {void show ();//abstract method in interface, default format: public abstract}class Outer {//Complement code public static Inter method () {//Subclass Object-- Subclass Anonymous Object Inter i = new Inter () {//Return value type is a reference type interface type Interpublic void Show () {System.out.println ("HelloWorld");}}; Return i;/* the above code equivalent to the following: return new Inter () {public void Show () {System.out.println ("Hello word!");}}; */}}class Outerdemo {public static void main (string[] args) {Outer.method (). Show ();//Returns an object. X.show ();/*1:outer.method () can see that method () should be a static method in Outer. 2:outer.method (). Show () can see that the return value of method () is an object. Because show () should be called by the object and because there is a show () method in interface Inter, I think the return value type of the method () is an interface. */}}



Interview question two:

/*
     interview:
         Ask to fill in the blanks separately output 30,20,10.
        
     Note:
        1: Inner and outer classes do not have an inheritance relationship.
        2: Qualifying This object with an external class name
            outer.this
*/

Class Outer {
public int num = 10;
Class Inner {
public int num = 20;
public void Show () {
int num = 30;
SYSTEM.OUT.PRINTLN (fill in the code);
SYSTEM.OUT.PRINTLN (fill in the code);
SYSTEM.OUT.PRINTLN (fill in the code);
}
}
}
Class Innerclasstest {
public static void Main (string[] args) {

Fill in the code.
}
}


Class Outer {public int num = 10;class Inner {public int num = 20;public void show () {int num = 30; SYSTEM.OUT.PRINTLN (num); System.out.println (This.num); System.out.println (Outer.this.num);}}} Class Innerclasstest {public static void main (string[] args) {//external class name. Inner Class Name Object name = Outer Class object. Inner class object; Outer. Inner oi = new Outer (). New Inner (); Oi.show ();}}


An object-oriented anonymous inner class question in the first season of Java entry

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.