练习代码(一)初始化与清理

1. 
public class Ltest {
public static void main(String[] args){
String[] ary = new String[5];
ary[0] = "e";
ary[1] = "a";
ary[2] = "b";
ary[3] = "c";
ary[4] = "d";
for(int i = 0; i < ary.length; i++)
System.out.println(ary[i]);
String[] ary2 = {"q","w","e","r","t"};
for(int j = 0; j<ary2.length; j++)
System.out.println(ary2[j]);
}

}

2.

class Shape{
void draw(){}
void erase(){}
void say(){}
}

class Circle extends Shape {
void draw(){
System.out.println("Circle.draw()");
}
void erase(){
System.out.println("Circle.erase()");
}
}


class Square extends Shape {
void draw(){
System.out.println("Square.draw()");
}
void erase(){
System.out.println("Square.erase()");
}
}

class Bing extends Shape{
void draw(){
System.out.println("Bing.draw()");
}
void erase(){
System.out.println("Bing.erase()");
}
void say(){
System.out.println("Bing.say()");
}
}

class Triangle extends Shape {
void draw(){
System.out.println("Triangle.draw()");
}
void erase(){
System.out.println("Triangle.erase()");
}
}

class RandomShapeGenerator {
private Random rand = new Random();
public Shape next(){
switch(rand.nextInt(4)){
default:
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
case 3: return new Bing();
}
}
}

public class Shapes {
private static RandomShapeGenerator gen = new RandomShapeGenerator();
public static void main(String[] args){
Shape[] s = new Shape[9];
for(int i=0;i<s.length;i++){
s[i] = gen.next();
}
for(int i=0;i<s.length;i++){
s[i].draw();
s[i].say();
}
}


}

3.public class test {
protected void finalize(){
System.out.println("Hello World!");
}
public static void main(String[] args){
new test();
System.gc();


}
}

4.

public class Tank {
boolean state = true;
void check(){
this.state = false;
}
    public void finalize(){
if(state)
System.out.println("error!");
}
public static void main(String[] args){
Tank tank = new Tank();
tank.check();
new Tank();
System.gc();
}
}

5.

public class test {
private static char m ='v' ;


test(int s){
this('r');
//this.test("Hello");
System.out.println(s);
}

test(char a){
m = a; 
System.out.println(a+"sdaf");
}

test(String b){
System.out.println(b);
}


public static void main(String[] args){
// char w = 'a';
new test(123);
}
}

6.编写一个类,内含为初始化的整型和字符型成员,打印其值以检验Java的缺省初始化动作

6.1

public class Tank {
private static int a;
private static char b;
public static void main(String[] args){
System.out.println("a="+a+" b="+b);
}
}

6.2public class Tank {
int a;
char b;
public void print(){
System.out.println("a="+a);
System.out.println("b="+b);
}
public static void main(String[] args){
new Tank().print();
}
}

7.编写一个类,内含未初始化的字符串引用,证明这个引用会被Java初始化为NULL.。



public class Test {
String a ;
public static void main(String[] args){
Test test = new Test();
System.out.println(test.a);
}
}

8,编写一个类,内含一个字符串字段,在定义处将其初始化;另一个字符串字段由构造器初始化。

public class Test {
private static String a = "Hello";
private static String b;
Test(){
b = "World";
}
public static void main(String[] args){
new Test();
System.out.println(a+b);
}
}

9.编写一个类,拥有两个静态字符串字段,其中一个在定义处初始化,另一个在静态快中初始化。现在,加入一个静态方法用以打印两个字段值。请证明他们都会在被指用之前完成初始化操作。

public class Test {
static String a = "Hello" , b;
static {
b = "World"; }
public static void main(String[] args){
System.out.println("a="+a+" b="+b);
}
}

10.编写一个类,内含字符串字段,并采用“实例初始化”方式初始化,试描述此种方式的用途。

public class Test {
String a = "Hello Java";
Test(){
System.out.println("a="+a);
}
Test(String s){
System.out.println("a="+a);
}
public static void main(String[] args){
new Test();
Test test = new Test("j");
}
}

11.编写一个方法,能够产生二维双精度型数组并加以初始化。数组的容量由方法的形式参数决定,其初值必须落在另外两个形式参数所指定的区间之内。编写第二个方法,打印出第一个方法所产生的数组。在mian()中通过产生不同容量的数组并打印其内容来测试出这两个方法。

public class Test {
int a , b;
public double[][] create(int a,int b,double c,double d){
double[][] array = new double[a][b];
double e = (d-c)/(a*b);
for(int i = 0; i < array.length; i++)
for(int j =0; j < array[i].length; j++){
array[i][j] = c;
c+=e;
}
return array;

}
public void print(double[][] array){
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++)
System.out.println(array[i][j]);
}
public static void main(String[] args){
Test array = new Test();
double[][] a1 = array.create(2, 5, 30.0, 60.0);
array.print(a1);

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值