Java written test--code correction

Source: Internet
Author: User
Tags instance method

 Package practice.javase;  Public Abstract class name {       private  String name;         Public Abstract Boolean isstupidname (String name) {};}

Error: Compilation cannot pass, reason abstract method cannot have method body, correct, change abstract method to common method, or remove method body;

 package   practice.javase;  public  abstract  class   Name { private   String name;  public  abstract  boolean   Isstupidname (String name);}  
 package   practice.javase;  public  abstract  class   Name { private   String name;  public  boolean   Isstupidname (String name) { return  false  ;};}  
 Package practice.javase;  Public class Something {       void  dosomething () {              private String s = "";               int L = s.length ();       }}

Error: Compile cannot pass, cause local variable cannot have permission modifier, permission modifier is set for setting access permission, local variable access permission has been determined in the method body, so error, correct, remove permission modifier;

 Package practice.javase;  Public class Something {       void  dosomething () {              = "";               int L = s.length ();       }}
 Package practice.javase;  Public Abstract class Something1 {       privateabstract  String dosomething ();}

Error: Compilation cannot pass, the abstract method in abstract class is to be inherited and rewritten by its subclass, so the permission can only be public or protected, or the default permission, cannot make private,private cannot be accessed by other class, and the abstract intention conflict, meaningless;

 Package practice.javase;  Public Abstract class Something1 {       abstract  String dosomething ();}
 package   practice.javase;  public  class   Something2 { public  static  void   main (string[] args) {              Something2 something2  = new   Something2 ();              Something2.dostring ();       System.out.println ( "something2.dostring return" +dostring ());               public   String dostring () {        return  "do somestring ..." ; }}

Error: A non-static method or member variable cannot be called in a static method, and can only be called through an object if it is to be called, because he is an instance method, not a class method;

 package   practice.javase;  public  class   Something2 { public  static  void   main (string[] args) {              Something2 something2  = new   Something2 ();              Something2.dostring ();       System.out.println ( "something2.dostring return" +something2.dostring ());               public   String dostring () {        return  "do somestring ..." ; }}
the filename of the Somestring3 class here is Otherstring.java  Package practice.javase;  Public class Something3 {       privatestaticvoid  main (string[] something_to_do) {              System.out.println ("Do Something ....");        }

Error: This code compile will not error, but we note a problem Somestring3 class filename is Otherstring.java, then it must be incorrect, Java mechanism is compiled after the generation of. Java files, interpreted as binary files

 Package practice.javase;  Public class otherstring {       privatestaticvoid  main (string[] something_to_do) {              System.out.println ("Do Something ....");        }
 Packagepractice.javase; Public InterfaceA {intx = 0;} Packagepractice.javase; Public classB {intx = 1;} Packagepractice.javase; Public classCextendsBImplementsa{ Public voidPX () {System.out.println (x); }        Public Static voidMain (string[] args) {NewC (). PX (); }}

Class C Compile Error:
The meaning of the interface is understood: the interface can be understood as a unified "protocol", and the interface attribute is also the content of the protocol, but the interface properties are public, static, the final
Member features of the interface:
A: Member variables can only be constants. Default modifier public static final
B: The member method can only be an abstract method. Default modifier public abstract
Recommended: Always manually give the modifier.
Inherits B in Class C, inherits the methods and properties of non-private in B, but invokes the member variables and methods in the parent class through the Super keyword;

 Package practice.javase;  Public class extends Implements a{       publicvoid  PX () {              System.out.println (Super. x);       }         Public Static void Main (string[] args) {              new  C (). PX ();       }}
 Packagepractice.javase; Public InterfacePlayable {voidplay ();} Packagepractice.javase; Public Interfacebounceable {voidplay ();} Packagepractice.javase; Public InterfaceRollableextendsplayable, bounceable{ ball Ball=NewBall ("Pingpang");} Packagepractice.javase; Public classBallImplementsrollable{PrivateString name;  PublicBall (String name) {Super();  This. Name =name; }        PublicString GetName () {returnname; }               Public voidPlay () { ball=NewBall ("FootBall");       System.out.println (Ball.getname ()); }} Error: compilation error, error in Ball=NewBall ("FootBall");, variable not declared,
 Packagepractice.javase; Public classBallImplementsrollable{PrivateString name;  PublicBall (String name) {Super();  This. Name =name; }        PublicString GetName () {returnname; }               Public voidPlay () { ball ball=NewBall ("FootBall");       System.out.println (Ball.getname ()); }       }

Java written test--code correction

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.