急急急!在线等JAVA高手,帮忙解答下几面2道题,我们马上交作业了,不会做,万分感激
1、指出下列Java程序中的错误,并修改使之能够正确运行abstractclassStudent{abstractvoidisPass(){};}classUnderGr...
1、指出下列Java程序中的错误,并修改使之能够正确运行
abstract class Student{
abstract void isPass( ) { };
}
class UnderGraduate extends Student {
public void isPass( ) {System.out.println("Pass!");}
}
public class AbstarctTest{
public static void main(String args[]) {
Student s;
s=new Student();
}
2.请在下面Java程序的划线处填上适当语句,使程序能够正常运行
public class MyClass {
public static void main(String args[]) {
try{ myMethod( );
}catch(MyException e) {
System.out.println(e.getMessage( ));
}
}
public----------------void MyMethod(
) ---------------- { //方法中声明抛弃用户自定义异常
throw ---------------- ;
}
}
class MyException extends---------------- { //用户自定义异常类
public String toString( ){
return "用户自定义异常";
}
} 展开
abstract class Student{
abstract void isPass( ) { };
}
class UnderGraduate extends Student {
public void isPass( ) {System.out.println("Pass!");}
}
public class AbstarctTest{
public static void main(String args[]) {
Student s;
s=new Student();
}
2.请在下面Java程序的划线处填上适当语句,使程序能够正常运行
public class MyClass {
public static void main(String args[]) {
try{ myMethod( );
}catch(MyException e) {
System.out.println(e.getMessage( ));
}
}
public----------------void MyMethod(
) ---------------- { //方法中声明抛弃用户自定义异常
throw ---------------- ;
}
}
class MyException extends---------------- { //用户自定义异常类
public String toString( ){
return "用户自定义异常";
}
} 展开
展开全部
考试过后自己要去调试一下
1.
################################
abstract class Student{
abstract void isPass( ) { };
}
class UnderGraduate extends Student {
public void isPass( ) {System.out.println("Pass!");}
}
public class AbstarctTest{
public static void main(String args[]) {
Student s;
//s=new Student();
s = new UnderGraduate();
}
2.
################################
public class MyClass {
public static void main(String args[]) {
try{ myMethod( );
}catch(MyException e) {
System.out.println(e.getMessage( ));
}
}
public static void MyMethod() throws MyException { //方法中声明抛弃用户自定义异常
throw new MyException() ;
}
}
class MyException extends Exception { //用户自定义异常类
public String toString( ){
return "用户自定义异常";
}
}
展开全部
s=new Student();
改成s=new UnderGraduate();
static
MyException
new MyException();
Exception
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class AbstarctTest {
public static void main(String args[]) {
Student s = new UnderGraduate();
}
}
public abstract class Student {
abstract void isPass();
}
public class UnderGraduate extends Student {
public void isPass( ) {
System.out.println("Pass!");
}
}
public class MyException extends RuntimeException {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public MyException() {
super();
}
public MyException(String message) {
super(message);
this.msg = message;
}
}
public class MyClass {
public static void main(String args[]) {
try{
myMethod();
}catch(MyException e){
System.out.println(e.getMessage() +" ===");
}
}
public static void myMethod(){
if(true){
throw new MyException("this is test");
}
}
}
public static void main(String args[]) {
Student s = new UnderGraduate();
}
}
public abstract class Student {
abstract void isPass();
}
public class UnderGraduate extends Student {
public void isPass( ) {
System.out.println("Pass!");
}
}
public class MyException extends RuntimeException {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public MyException() {
super();
}
public MyException(String message) {
super(message);
this.msg = message;
}
}
public class MyClass {
public static void main(String args[]) {
try{
myMethod();
}catch(MyException e){
System.out.println(e.getMessage() +" ===");
}
}
public static void myMethod(){
if(true){
throw new MyException("this is test");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public abstract class Student {
abstract void isPass();
}
s=new Student();改成
s=new UnderGraduate();
static throws MyException MyException Exception
abstract void isPass();
}
s=new Student();改成
s=new UnderGraduate();
static throws MyException MyException Exception
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
abstract void isPass( ) { }; 去掉{}变成抽象方法
s=new Student(); 不能直接new 抽象类实例,new UnderGraduate()
s=new Student(); 不能直接new 抽象类实例,new UnderGraduate()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一题:原因,抽象方法后面不能有方法体,并且抽象类不能new对象,public static void main(String args[]) { UnderGraduate u=new UnderGraduate (); u.isPass();}
第二题 throw new RuntimeException;
第二题 throw new RuntimeException;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询