因为我是从C++转来的,所以学起来比较容易,闲话少说直接上题。
PS:编译器eclipse,正在安装IDEA,安装成功的话后面用这个编译器写
1.使用简短的和正常的打印语句来编写一个程序:
PS:这个代码包我没下载,自然这道题写不了,也无所谓这个包没啥用。
2.创建一个包含float域的类,并用这个类展示别名机制。
首先,我们要明白域是一种属性,可以是一个类变量,一个对象变量,一个对象方法变量或者是一个函数的参数。
import java.util.*;
public class TEST {
static class A{
float i;
}
public static void main(String[] args) {
A a=new A();
A b=new A();
a.i=1;
b.i=2;
System.out.print(a.i+" "+b.i+'\n');
a=b;
System.out.print(a.i+" "+b.i);
}
}
output:
1.0 2.0
2.0 2.0
3.创建一个包含一个float域的类,并用这个类展示方法调用时的别命
import java.util.*;
class A{
float i;
}
public class TEST {
public static class B{ //不写static会报错
static void f(A a) { //The method f cannot be declared static;
a.i=2.10f; // static methods can only be declared in
} //a static or top level type
}
public static void main(String[] args) {
A b=new A();
b.i=3.10f;
System.out.print(b.i+" ");
B.f(b);
System.out.print(b.i);
}
}
output:3.1 2.1
4.编写一个计算速度的程序,它所使用的距离和时间都是常量。
import java.util.*;
public class TEST {
public static void main(String[] args) {
final float time=10;
final float meter=5;
System.out.print(meter/time);
}
}
output:0.5
5.创建一个名为Dog的类,它包含两个String域:name和says。在main()方法中,创建两个Dog对象