java编程题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java编程题相关的知识,希望对你有一定的参考价值。

编码定义一个公有类MyClassA,它含有两个单精度浮点类型(float型)变量fvar1和fvar2;一个整型(int型)变量ivar1,该类中还有一个名为sum的公有(public)方法,它将fvar1和fvar2的值相加,结果放在ivar1中并返回ivar1的值。
编码定义一个类MyClassB,它派生自MyClassA。MyClassB类重写其父类的sum方法,将fvar1与fvar2的平均值赋值给ivar1,并返回ivar1的值。

参考技术A public class MyClassA
private float fvar1;

private float fvar2;
private int ivar1;

public int sum()
ivar1=fvar1+fvar2;

return ivar1;

public float getFvar1()return fvar1;

public float getFvar2()return fvar2;

public int getIvar1()return ivar1;

public void setFvar1(float fvar1)this.fvar1=fvar1;
public void setFvar2(float fvar2)this.fvar2=fvar2;

public void setIvar1(int ivar1)this.ivar1=ivar1;


public class MyClassB extends MyClassA
@overwrite

public int sum()
ivar1= (fvar1+fvar2)/2;

return ivar1;



参考技术B public class MyClassA
float fvar1=2;
float fvar2=3;
static int ivar1=0;

public static void main(String[] args)

MyClassA myclassA=new MyClassA();
myclassA.sum();
System.out.println(ivar1);


public int sum()

float sum=0;
sum=fvar1+fvar2;
ivar1=(int)sum;
return ivar1;



输出结果为:5
参考技术C public
class MyClassA

privatefloatfvar1;

privatefloatfvar2;

protectedintivar1;

publicint sum()

ivar1 = (int) (fvar1 + fvar2);

returnivar1;





class
MyClassB extends MyClassA

publicint sum()

ivar1 = super.sum() / 2;

returnsuper.ivar1;


本回答被提问者和网友采纳
参考技术D public class MyClassA
float fvar1;
float fvar2;
static int ivar1;

public int sum()

float sum=0;
sum=fvar1+fvar2;
ivar1=(int)sum;
return ivar1;


class MyClassB extends MyClassA
float fvar1;
float fvar2;
int ivar1;
public int sum()
float sum = 0 ;
sum = (fvar1+fvar2)/2;
ivar1=(int)sum;
return ivar1;

以上是关于java编程题的主要内容,如果未能解决你的问题,请参考以下文章

Java编程题

java编程题

java编程题

java 编程题集

JAVA编程题(AWT计算器)

JAVA TCP Socket编程 计算题