老师课堂上临时给了道题.写完下课就交了.我没有编写出来.讲到了抽象类和接口,因为前面有些部分没有跟上,以至于这节课上的东西学得有点紧.
题目是大意是:定义一个接口,有两个变量X和Y,X=55,Y=45,再定义两个类,分别实现接口,类1要实现X加Y,类2实现X减Y,还有入口.本来是要求有返回值的,大多数同学都不怎么会,于是说直接输出.求会的有空帮忙写下程序,我现在有点迷糊,把自己上课写的程序拿回来运行,有好多问题.学JAVA有几周了,可是还没有正式独立的写过程序,有点抽象,觉得有点空.希望能帮忙写出来,我有点更新的认识.
还有,真的.这个帐号是我以前注册的,以前只是感兴趣,并没有接触过编程,现在真正切切的要学编程做为以后的求生之道了.我真的很想学好这门编程.只是有时候学起来感觉很困惑,希望大家帮帮忙.分可能会给的少些,我本来有20分,下了两个实例扣了三分.现在只剩下了14分.希望大家帮帮我.以后我补加也行.谢谢.
题目是大意是:定义一个接口,有两个变量X和Y,X=55,Y=45,再定义两个类,分别实现接口,类1要实现X加Y,类2实现X减Y,还有入口.本来是要求有返回值的,大多数同学都不怎么会,于是说直接输出.求会的有空帮忙写下程序,我现在有点迷糊,把自己上课写的程序拿回来运行,有好多问题.学JAVA有几周了,可是还没有正式独立的写过程序,有点抽象,觉得有点空.希望能帮忙写出来,我有点更新的认识.
还有,真的.这个帐号是我以前注册的,以前只是感兴趣,并没有接触过编程,现在真正切切的要学编程做为以后的求生之道了.我真的很想学好这门编程.只是有时候学起来感觉很困惑,希望大家帮帮忙.分可能会给的少些,我本来有20分,下了两个实例扣了三分.现在只剩下了14分.希望大家帮帮我.以后我补加也行.谢谢.
aa a = new aa();
bb b = new bb();
System.out.println(a.x_y());
System.out.println(b.x_y1());
}}
interface It1
{
public int x = 55;
public int y = 45;
//int x_y();
//int x_y1();
}
class aa implements It1{
public int x_y(){
return x-y;
}
}
class bb implements It1{
public int x_y1(){
return x+y;
}
}
也是初学;写的不好;呵呵
int xy(int x, int y);
}
class plus implements pm {
int xy(int x, int y) { return x+y;}
}
class minus implements pm {
int xy(intx, int y) { return x-y; }
}
public static void main(String[] args){
A a = new A();
System.out.println(a.Calculate());
B b =new B();
System.out.println(b.Calculate());
}
}interface Impl{
int x = 45;
int y = 55;
int Calculate();
}class A implements Impl{ public int Calculate() {
return this.x+this.y;
}
}class B implements Impl{
public int Calculate() {
return this.x-this.y;
}
}
package test; // test 包下public interface Test { //定义接口
public int XaddY(int a,int b); //返回int类型,带两个int类型的参数
public int XdleY(int a,int b);
}下面是实现类;
package test;public class TestImpl implements Test {
int result =0; // 定义成员变量
int result2;
public int XaddY(int a, int b) { // 实现方法
result = a+b;
return result;
} public int XdleY(int a, int b) {
return result2 = a-b;
}
public static void main(String[] args) {
TestImpl impl = new TestImpl();
int num1 = impl.XaddY(3, 2);
int num2 = impl.XdleY(3, 2);
System.out.println(num1);
System.out.println(num2);
}
}
打印结果:
5;
1;
private int x;
private int y;
pulic int Res(int x,int y);
}class A inperment Test{
public int Res(int x,int y){
result x+y;
}
}class B inperment Test{
public int Res(int x,int y){
result x-y;
}
}public class R{
public static void main(String [] args){
A a=new();
B b=new();
System.out.println(a.Res(2,3));
System.out.println(b.Res(2,3));
}
}
我也是刚开始学习java,不知道我写的对不对,请大家帮忙看下
public static final int x=55;
public static final int y=45;
}
package day;public class Interface1 implements Interface{
public int numble(){
return x+y;
}
}
package day;public class Interface2 implements Interface {
private double abc(){
return x-y;
}
public static void main(String[] args) {
Interface1 a=new Interface1();
System.out.println(a.numble());
Interface2 b=new Interface2();
System.out.println(b.abc()); }}
不知道是不是符合你的那个题目,反正我认为就是这样的。我看5楼和9楼那个,我都不敢相信我这个了!汗!请多多请教!我是分开写的
final int x=55,y=45;
void yunsuan();}class lei1 implements jiekou{
public void yunsuan(){
System.out.println("x+y="+(x+y));
}
}class lei2 implements jiekou{
public void yunsuan(){
System.out.println("x-y="+(x-y));
}
}public class example{
public static void main(String args[])
{
lei1 a=new lei1();
a.yunsuan();
lei2 b=new lei2();
b.yunsuan();
}
} 我这个最容易懂了