码迷,mamicode.com
首页 > 编程语言 > 详细

Java笔试--代码纠错

时间:2017-07-26 21:48:32      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:ext   ble   super关键字   cti   统一   修饰符   name   turn   关键字   

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 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();
       }
}

错误:编译无法通过,原因局部变量不能有权限修饰符,权限修饰符是为了设置访问权限而设定的,局部变量的访问权限已经确定是在方法体内了,故错误,改正,去掉权限修饰符;

package practice.javase;
public class Something {
       void doSomething(){
              String s = "";
              int l = s.length();
       }
}

package practice.javase;
public abstract class Something1 {
       private abstract String doSomething();
}

错误:编译无法通过,原因抽象类中的抽象方法是要被其子类继承和重写的,所以权限只能是public或者protected,或者默认权限,不能使private,private不能被其他类访问,和抽象本意冲突,没有意义;

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.....";
       }
}

错误:静态方法中不能调用非静态的方法或成员变量,如果想调用,只能通过对象调用,因为他是实例方法不是类方法;

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.....";
       }
}

此处Somestring3类的文件名是OtherString.java
package practice.javase;
public class Something3 {
       private static void main(String[] something_to_do){
              System.out.println("Do Something....");
       }
}

错误:这段代码编译不会报错,但是我们注意一个问题Somestring3类的文件名是OtherString.java,那么就一定不正确,java的机制是编译后生成.java文件,解释器解释成二进制文件

package practice.javase;
public class OtherString {
       private static void main(String[] something_to_do){
              System.out.println("Do Something....");
       }
}

package practice.javase;
public interface A {
       int x = 0;
}
package practice.javase;
public class B {
       int x = 1;
}
package practice.javase;
public class C extends B implements A{
       public void pX(){
              System.out.println(x);
       }
       public static void main(String[] args) {
              new C().pX();
       }
}

Class C编译报错:
接口的含义理解:接口可以理解成统一的"协议",而接口中的属性也属于协议中的内容;但是接口的属性都是公共的,静态的,最终的
接口的成员特点:
A:成员变量 只能是常量。默认修饰符 public static final
B:成员方法 只能是抽象方法。默认修饰符 public abstract
推荐:永远手动给出修饰符。
在Class C中继承了B,继承了B中非私有的方法和属性,但要通过super关键字来调用父类中的成员变量和方法;

package practice.javase;
public class C extends B implements A{
       public void pX(){
              System.out.println(super.x);
       }
       public static void main(String[] args) {
              new C().pX();
       }
}

package practice.javase;
public interface Playable {
       void play();
}

package practice.javase;
public interface Bounceable {
       void play();
}

package practice.javase;
public interface Rollable extends Playable, Bounceable{
       Ball ball = new Ball("PingPang");
}

package practice.javase;
public class Ball implements Rollable{
       private String name;
       public Ball(String name) {
              super();
              this.name = name;
       }
       public String getName() {
              return name;
       }
       
       public void play(){
              ball = new Ball("FootBall");
              System.out.println(ball.getName());
       }
       
}

错误:编译错误,错误在ball = new Ball("FootBall");,变量未经声明,
package practice.javase;
public class Ball implements Rollable{
       private String name;
       public Ball(String name) {
              super();
              this.name = name;
       }
       public String getName() {
              return name;
       }
       
       public void play(){
              Ball ball = new Ball("FootBall");
              System.out.println(ball.getName());
       }
       
}

 

Java笔试--代码纠错

标签:ext   ble   super关键字   cti   统一   修饰符   name   turn   关键字   

原文地址:http://www.cnblogs.com/lin-jing/p/7241621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!