如有错误,欢迎各位大佬在评论区指正
1.题目
class A
{ int m=10,sum=0;
void f( )
{ int i=0;
if(m>9)
/********************************/
{ int z;
z=2*m+z;
}
for(;i<m;i++)
{
sum=sum+i;
}
m=sum;
/*********************************/
z=i+sum;
}
}
public class E_1 {
public static void main (String args[]) {
System.out.println("大家好!");
}
}
1.答案
int z 改成 int z=0或赋其它值
z=i+sum; 改成 sum=i+sum;或者m=i+sum;
2.题目
class Point {
int x,y;
/*****************************/
Point(float a,float b) {
x=a;
y=b;
}
}
public class E_2 {
/***************************************/
void main(String args[]) {
Point p1,p2;
p1=new Point(10,10);
p2=new Point(23,35);
}
}
2.答案
Point(float a,float b) 改成 Point(int a,int b)
void main(String args[]) 改成 public static void main(String args[])
3.题目
class Computer{
/***************************/
void add(int x,int y){
return x+y;
}
}
public class E_3 {
public static void main(String args[]){
Computer com = new Computer();
int m = 100;
int n = 200;
/*************************************/
int result = add(m,n);
System.out.println(result);
}
}
3.答案
void add(int x,int y) 改成 int add(int x,int y)
int result = add(m,n); 改成 int result =com. add(m,n);
4.题目
class A{
int i;
public A(int j)
{i=j;}
}
class B
{ int a;
static int b;
void f( int x, int y)
{ a=x;
b=y;
}
static void g( int z)
{ b=23;
/*********************************/
a=z;
}}
public class E_4
{
public static void main(String args[])
{ int c=10;
/***********************************/
A aa=new A();
}
}
4.答案
a=z; 改成 b=z;
A a=new A( c );
5.题目
class Person
{ String name ;
static String country=“A城”;
public Person(String name){
/******************************/
name = name ; //this.name = name ;
}
public void info(){
System.out.println("姓名:" + this.name + ",年龄:" + ",城市:" + country) ;}
}
public class E_5{
public static void main(String args[]){
Person p1 = new Person("张三") ;
Person p2 = new Person("李四") ;
Person p3 = new Person("王五") ;
p1.info() ;
p2.info() ;
p3.info() ;
/********************************/
country="B城";
p1.info() ;
p2.info() ;
p3.info() ;
}
}
5.答案
name = name 改成 this.name = name
Person.country=“B城”;
6.题目
class A
{ private String name ;
public void setName(String name)
{this.name = name ;}
public String getName()
{return this.name ;}
}
/***************************************/
class B ,A
{ public void say()
{System.out.println(getName());}
}
public class E_6
{public static void main(String args[])
{ B b=new B();
/******************************/
A.setName("张三");
b.say();
}
}
6.答案
class B ,A 改成 class B extends A
A.setName(“张三”); 改成 b.setName(“张三”);
7.题目
class People {
private int averHeight = 166;
public int getAverHeight() {
return averHeight;
}
}
/****************************************/
class ChinaPeople , People {
int height;
public void setHeight(int h) {
height = h;
}
public int getHeight() {
return height;
}
}
public class E_7{
public static void main(String args[]) {
ChinaPeople zhangSan = new ChinaPeople();
/***********************************************/
System.out.println("子类对象未继承的averageHeight的值是:"+zhangSan.averHeight);
zhangSan.setHeight(178);
System.out.println("子类对象的实例变量height的值是:"+zhangSan.getHeight());
}
}
7.答案
class ChinaPeople , People 改成 class ChinaPeople extends People
System.out.println(“子类对象未继承的averageHeight的值是:”+zhangSan.averHeight); 改成 System.out.println(“子类对象未继承的averageHeight的值是:”+zhangSan.getAverHeight());
8.题目
class Goods {
public double weight;
public void oldSetWeight(double w) {
weight=w;
System.out.println("double型的weight="+weight);
}
public double oldGetPrice() {
double price = weight*10;
return price;
}
}
/***************************************************/
class CheapGoods , Goods {
public int weight;
public void newSetWeight(int w) {
weight=w;
System.out.println("int型的weight="+weight);
}
public double newGetPrice() {
double price = weight*10;
return price;
}
}
public class E_8 {
public static void main(String args[]) {
CheapGoods cheapGoods=new CheapGoods();
/**************************************************/
cheapGoods.weight=198.98;
System.out.println("对象cheapGoods的weight的值是:"+cheapGoods.weight);
System.out.println("cheapGoods用子类新增的优惠方法计算价格:"+
cheapGoods.newGetPrice());
cheapGoods.oldSetWeight(198.987); //子类对象调用继承的方法操作隐藏的double型变量weight
System.out.println("cheapGoods使用继承的方法(无优惠)计算价格:"+
cheapGoods.oldGetPrice());
}
}
8.答案
class CheapGoods , Goods 改成 class CheapGoods extends Goods
cheapGoods.weight=198.98; 改成 cheapGoods.weight=198;