java学习笔记3-thinking in java

//初始化和清除
//1.用构建器自动初始化
//:SimpleConstructor.java
// Demonstration of a simple constructor
package c04;


class Rock {
Rock() { //This is the constructor
Systemout.println(Creating Rock);
}
}


public class SimpleConstructor {
public static void main(String[] args) {
for(int i=0;i<10;i++)
new Rock();
}
}


//2.方法过载
//Overloading.java
// Demonstration of both constructor(过载构建器和过载的原始方法)
import java.util.*;
class Tree {
int height;
Tree() {
prt("Planting a seedling");
height = 0;
}
Tree(int i) {
prt("Creating new Tree hat is" + i + "feet tall");
height = i;
}
void info() {
prt("Tree is" + height + "feet tall");
}
void infor(String s) {
prt(s + "Tree is " + height + "feet tall");
}
}


public class Overloading {
public static void main(String[] args) {
for(int i = 0;i < 5; i++) {
Tree t=new Tree(i);
t.info();
t.info("overloaded method");
}
new Tree();
}
}


//3.主类型过载
// 若我们的数据类型“小于”方法中使用的自变量,就会对那种数据类型进行“转型”处理;
// 若我们的自变量范围比它宽,就必须用括号中的类型名将其转为适当的类型,否则编译器会报错


//4.返回值过载:不能根据返回值类型来区分过载的方法


//5.默认构建器:若创建一个没有构建器的类,则编译程序会帮我们自动创建一个默认构建器
//DefaultConstructor.java


class Bird {
int i;
}


public class DefaultConstructor {
public static void main(String[] args) {
Bird nc = new Bird(); //default
}
}


//6.this关键字
//用于那些特殊的类-需明确使用当前对象的句柄
//:Leaf.java
// Simple use of the "this" keyword


public class Leaf {
private int i = 0;
Leaf increment() {
i++;
return this; //将句柄返回给当前对象
}
void print() {
Systemout.println("i=" + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
//因为increment()通过this关键字返回当前对象的句柄,
//所以可方便的对同一个对象执行多项操作
}



//:Flower.java
// Calling constructors with "this" 在一个构建器里调用另一个构建器


public class Flower {
private int petalCount = 0;
private String s = new String("null");
Flower(int petals) {
petalCount = petals;
Systemout.println("Constructor w/ int arg only,petalCount=" + petalCount);
}
Flower(string ss) {
Systemout.println("Constructor w/ String arg only,s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals); 
//! this(s) //会报错,可用this调用一个构建器,但是不能调用两个
this.s = s;//由于s的名字和成员数据s名字相同,所以会出现混淆,可用this.s来引用成员数据
Systemout.println("String &int args");
}
Flower() {
this("hi",47);
Systemout.println("default constructor(no args)");
}
void print() {
//! this(11) //不能从一个构建器之外的其他任何方法内部调用构建器
Systemout.println("petalCount=" + petalCount + "s=" + s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
}


//6.成员初始化
// 若程序员未初始化就用变量,则会收到出错提示消息,且会被赋予默认值
// 若对象句柄未初始化,则句柄会获得一个空值


//7.数组初始化
//:Arrays.java
// Arrays of primitives


public class Arrays {
public static void main(String[] args) {
int[] a1 = {1,2,3,4,5};
in[] a2;
a2=a1;
for(int i=0;i<a2.length;i++) {
a2[i]++;
}
for(int i = 0;i<a1.length;i++) {
prt("a1[" + i + "]=" + a1[i]);
}
}
static void prt(String s) {
Systemout.println(s);
}
}


//8.多维数组
//:MultiDimArray.java
// Grating multidimensional arrays
import java.util.*;


public class MultiDimArray {
static Random rand = new Random();
static int pRand(int mod) {
return Math.abs(rand.nexInt())%mod + 1;
}
public static void main(String[] args) {
int[][] a1 = {
{1,2,3,},
{4,5,6,},
};
for(int i = 0;i<a1.length;i++)
for(int j = 0;j<a1[i].length;j++)
prt("a1[" + i + "][" + j + "]=" + a1[i][j]);
int[][][] a2 = new int[2][2][4];
for(int i = 0;i<a2.length;i++)
for(int j = 0;j<a2[[i].length;j++)
for(int k = 0;k<a2[i][j].length;k++)
prt("a2[" + i + "][" + j + "][" + k +"]=" + a[i][j][k]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值