JAVA语言学习笔记(一)
1 一切都是对象
JAVA中所有代码都必须写在类里面。
方法名和参数列表(它们合起来被称为"方法签名")唯一地标识出某个方法。联想多态。
基本数据类型的"局部变量"相对于类的数据成员不会自动初始化,但会在编译时报错误。
面向对象的程序设计通常可简单的归为"向对象发送消息"。
JAVA中,除了基本数据类型,其它对象传递的是引用。
static域或方法,有特定的存储空间和可以直接用类直接调用(static字段对每个类来说都只有一份存储空间,而非static字段则是对每个对象有一个存储空间)。
2 操作符
观察一下代码,猜测输出的结果。
class Tank {
int level;
} public class Think {
public static void main(String[] args) {
Tank t1 = new Tank();
Tank t2 = new Tank();
t1.level = 9;
t2.level = 47;
System.out
.println("1: t1.level:" + t1.level + ", t2.level:" + t2.level);
t1 = t2;
System.out
.println("2: t1.level:" + t1.level + ", t2.level:" + t2.level);
t1.level = 27;
System.out
.println("3: t1.level:" + t1.level + ", t2.level:" + t2.level);
}
}
重点在13行,内部做了什么?
结果如下所示:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQMAAAA9CAIAAAAmtJv1AAADmUlEQVR4nO2d25WDIBRFqcuCrIdqbMYm7ODMB0lEeSgaIjp7f02i4uXi4RE5a8w0TdM0CeB/Y1ACgFACgAMlAEgrJQy9McZ0dqx0s6LyR9sZUzGeA+W7+I0xph9qhPSOaFdIDeZH0itJXn4+laoa7XlmJYy2M/2goT8fq+0iT8qx8kfbVc1dQflDbzrr/Xk8qmh+vDKHfrfWGsqPJMl2preL+vkfakd7hmB2FH9SXW+4o3XGVRcQXHNeCf4tvEdz7nK8D6nzc+Un6jsu2nfog6L21iefn8i9NsprJD/6iDkd/e5qXcC3lSApX+HTSvC/GG33abzFmUP/CTZ1fqr89/VBfb0xYbTdmRnS1gNRILN28vMa85XW8X59X8FOJZRRUQlBn+q1x9y880W582PlbwXz6TvPtGru2kKRtZOfeRH1GXyCUBsWwu2UkO0vX7n2OrzN/vXozPXo7EhSOj+j7UqXlC3mJ/HIty2EWrOj9NqvsPywJbKr1aHv7OqK/Oq2aB7sH14cK0tPPD9DH5mmb96gxfxEldD4iOArYTW6LVulsKljPzcWlr8+3T+6HNETs1WP2Pm58pPrhPg93ZGSrjPMz8ZK+g75WYa1SkfzQuDN2jewXbs/k8NOUMJpot0s3A2UACChBAAHSgCQUAKAAyUASCgBwIESAKSVEoqcIge4u1NH0iVOFBw8P2Cx2+KAUyTKM506kio7UXDwXEh8dhTsEsGp8/q2lhMFB8+ualUkqoRwpy5OnV84UXDwXEighJN2LEkPder8wImCg+dCFko44BSJ8nCnTjUnCg6eC1n6E8qdIlEe7tTZ70TBwbO6pmEHz1sJxU6RLI906izD2uNEwcEThtWsg4c3axXBwXMjUEI1cPDcCpQAIKEEAAdKAJBQAoADJQBIKAHAgRIApNi+o9ebwBo3u7tTJ+qYwamzyVV5KyK+76iG4+TuTp2UYwanTp4f5O0rJJ06652Mif0kax7t1PGvih7DqfPrvH2PlRLeycCpk6tvYscyTp2L8vYV4mNCfkfuJo906vgBVLJePdKpsxl/G0JI/nZU5X/GvAu+q1Mn75jBqZM5uWrevsKsBH/nZLA0w6mTd8zg1EnGcyRvV+CPCZ4J49w64YFOna0VLU6deDyH8nYJvFmrCE6dG4ESqoFT51agBAAJJQA4UAKAhBIAHCgBQEIJAA6UACChBAAHSgCQUAKAAyUASCgBwIESACTpD5JUKFqZgV3QAAAAAElFTkSuQmCC" alt="" />
equals和==号的运用,特别注意String类的特点
class Value{ String i;
}
public class EqualsMethods {
public static void main(String[] args){
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = "10"; //"10"是一个字符串常量,存储在常量区 System.out.println(v1.equals(v2)); //false
System.out.println(v1.i.equals(v2.i)); //true
System.out.println(v1.i == v2.i); //true }
}
String类打印。 观察下面代码的第9行程序和注释的内容。也可以是(观察下面代码,判断是否发生错误?编译错误还是运行时错误?那类错误?)
package thinkJava; import java.util.ArrayList;
import java.util.List; public class InfiniteRecursion { public String toString(){
return "InfiniteRecursion address: " + super.toString() + "\n"; //this ---> super.toString()
}
public static void main(String[] args){
List<InfiniteRecursion> v = new ArrayList<InfiniteRecursion>();
for(int i = 0; i < 10; i++){
v.add(new InfiniteRecursion());
}
System.out.println(v);
}
}
//如果你真的想打印出对象的内存地址,而是应该调用Object.toString()方法。
//这才是负责此任务的方法。所以。你不该用this,而是应该调用super.toString()方法。
如果用this方法,则发生递归死循环,很快给出运行时错误--aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdwAAAAUCAIAAACoDRvPAAAEfUlEQVR4nO1bS7bjOgjUulgQ69G099GHffTYi+ENZBMkAbaTXDs3jzo9aUUBVILSL7f8+fuPE4lEIhGilHKRoxTlRCKR2EWKciKRSHwQUpQTiUTig5CinEgkEh+Ea0W5ApfS/YN6jfsdYOG6/D77jU96v2ELdGi+CLkURlpje3HUx3l7zu9SueArAZ6GlMCP5psJ2IoO8f11B4ULsDkm/PliRyUprRzewvNNenX5TrnCg6alXirKcG35vR1m/BWuEmVmpv35WupaCU0lL4vtLr/PQVfB9R6xF5d31QVYooyFcZuMCj9S74GMvM7zHXp1lyj35d0qqtVSKyq96uqVViZevqKXr7ZcV9w+sjrr5ZTDFVV2FrKZap0Rx/YApv2zduL4azXswOZ0DQANU3oKCB/tOlTdnw6L8jqPwMvWAlsYq6MtHs/vWd5mvzGg2H7NeFC1SIdGhRd/jFksTDtRntAjyVvCx95NeQryKhiXWY+8ifJjd4mGimFhCvn08tPLZ+zjH9yZo57rurXI4oHKlKlXQTxeu+nX6X+HKJv0LbWrYUmOYaWVetPZoFdgGARIj9/XvnnyQOUiqeNehU6jD9bh3PMJO95OWTgZ7LR0F+1oLHq8kdpbgvBPSuCOXV/IVzT5nWLi6poCvyrCo7xZfo+AsCfNiUf7InzkZBy/B0OUfb/meKWPHBF2AY5ymXnlxePVI2tR9hNb2Pb49PKTnXzmkztls66X+ghgHaaqKZO0IB677hw9sfp/xk65YV0k9TbH2ZrNK7xeqTS9+nR/XJTjSdLsvyLKZ+3sXl+M+jKZDXgbPiLLwtPHN/milJ+EbfrVo3udtxjzGO14SOWYStE4fg/zuDw75niHiTh7SB/EzswrO57wqASTeAWibPIZ5Cf7031clIO6biRAi5+6TZinV148Zt15fq3+t94pD5CDjK4Em24/OYaLrRTlDj6fpd+hXCTKjl9WLZeKchhP+6/e1u3G72Ecl2/niCif5oH2TpBePHuijDjeKZnXF9rswGdg/0dFGYEXYqiMwFS700CgV98pyqBOOut00nid19XMNqneuzz4jxii14TjCdc45jgb9jtF2Yr/nCg7vOm07q6P+urFZ1+fPVF2/W64WJR34qHxFnW3/3gRrEbhrXaDHW+83WVumSR+voDuw9YaMedVEI9Xj2JnKK7ooW/ic7Tf43VRZr+uCRlxveOGflE09SqIx2z3/Fr97/5JnNwwyh5Z9sv6+sw8HoJ1zAFgVF7sNxxhlux42Dm+xXEacOyftuPE79qZ/O7y9piajUAanG4vTmd1WZhEWq3JswZ5ft/K2y6r5gPjwIN8Omx5ov6mODr5ZtoJxts9iM377thvr4NGXYTjMutRfhKnH/Eagp/EzXyymZ9+PkPfvlo7Wdes3rT0AubplRuPH6d3HWT1/7o/HjF/l5NIeLj+12lvxxcMIaHwXaLsvS8nEh3UDuX3pkpm+5fiu0Q5kUgkfjlSlBOJROKDkKKcSCQS/0ekKCcSicQHIUU5kUgkPgj/AZPnWqg1BYbkAAAAAElFTkSuQmCC" alt="" />
正确的做法是用父类的super.toString()方法。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAADDCAIAAAAa4RzcAAAXQklEQVR4nO2d24GrOgxFUxcFpR5KuFXQDMVwP5KAZEnGDHkIn7W+ZhzHbyFjgvbtPwAAgGTcFgAAgGTgnAAAIB04JwAASAfOCQAA0oFzAgBoY7rfHgzjr5vSP5tzmsfhOerzM2WdiCf3qaVE+S35jekuim4r51D+WjsyLaaz/TpbuTsUUXpeHsu1siTbx3kc1LJ3qjlqA+fA7j7BG+xuHodXGdPdTst09ybLnZdHYtye0h63ZegvhuvZbwvKObmDFaX7zOPwyTEahwNXB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6cEwAApAPnBAAA6cA5AQBAOnBOAACQjv8B71Il1LiJilUAAAAASUVORK5CYII=" alt="" />
3 接口
抽象类,枚举、继承、类型、多态、循环、import方式
package thinkJava; import testzd.Note; //testzd package中的Note.java文件 abstract class Instrument {
private int i; public abstract void play(Note n); public String what() {
return "Instrument";
} public abstract void adjust(); } // 继承抽象类,抽象方法一定要实现。如果不实现父类中的抽象方法,则子类必须也是抽象类,否则编译通不过。
class Wind extends Instrument {
public void play(Note n) {
System.out.println("Wind.pay() " + n);
} public String what() {
return "wind";
} public void adjust() {
};
} class Percussion extends Instrument {
public void play(Note n) {
System.out.println("Percussion.pay() " + n);
} public String what() {
return "Percussion";
} public void adjust() {
}
} class Stringed extends Instrument {
public void play(Note n) {
System.out.println("Stringed.play() " + n);
} public String what() {
return "Stringed";
} public void adjust() {
}
} class Brass extends Wind {
public void play(Note n) {
System.out.println("Brass.play() " + n);
} public void adjust() {
System.out.println("Brass.adjust()");
}
} class Woodwind extends Wind {
public void play(Note n) {
System.out.println("Woodwind.play() " + n);
} public String what() {
return "Woodwind";
}
} public class Music4 { static void tune(Instrument i) {
i.play(Note.MIDDLE_C);
i.play(Note.C_SHARP);
i.play(Note.B_FLAT);
} static void tuneAll(Instrument[] e) {
for (Instrument i : e) { //遍历的方式
tune(i);
}
} public static void main(String[] args) {
Instrument[] orchestra = { new Wind(), new Percussion(),
new Stringed(), new Brass(), new Woodwind() }; //Instrument 类型的,但是放进了好多子类型的。父类是抽象的,不能实例化。保证安全。
tuneAll(orchestra);
} }
//猜测下输出的结果,以及为什么这么繁琐的搞这么多的类?
testzd package中的Note.java文件如下所示
package testzd; public enum Note {
MIDDLE_C, C_SHARP, B_FLAT; // Etc.
}
运行的结果是:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARcAAAEACAIAAAAWYqWgAAAT00lEQVR4nO2d2aGDIBBFrcuCrMdqbMZi8j5cmIEZBEmi5p3zlRBFRK8szg3dCwDa6K4uAMDjQUUAraAigFa6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W59LZdUFEruPTU9/D5iy49LZuiHWtU5Lr0MqWHKnDpdde69NIZ50MN2S46c828MRpATfLFxonxGJj88ltXXHrwHX5WRbj04Gv8rIoAvgYquiW49B4FKgJoBRUBtPIH0TSWsrJycGwAAAAASUVORK5CYII=" alt="" />
abstract 修饰的类中可以没有抽象方法,即所有的方法全部都有实现体。但是依旧不能为抽象类定义对象。
interface关键字使抽象的概念更向前迈进了一部。interface是一个完全抽象的类。
1 interface中可以有字段和方法
2 字段隐式地是static 和final的
3 方法隐式地是public的
如下是一个接口的定义
interface Inst {
int VALUE = 5; // static & final // cannot have method definitions
void play(Note n); // Automaticaly public void adjust();
}
foreach迭代,可变参数列表
class A{}
public class TestObject{
static void printArray(Object...args){
for(Object obj:args){
System.out.print(obj + " ");
}
System.out.println();
}
public static void main(String[] args){ printArray(new Integer(47), new Float(12.3F), new Double(23.45));
printArray(47, 3.14F, 11.11);
printArray(new A(), new A(), new A());
printArray((Object[])new Integer[]{1, 2, 3, 4});
printArray(); //Empty list is Ok. }
}
可变参数列表
输出结果
47 12.3 23.45
47 3.14 11.11
thinkJava.A@1d1acd3 thinkJava.A@a981ca thinkJava.A@8814e9
1 2 3 4
结果
4 构造器与重载
默认构造器只有在没有明确创造构造器时候才会起作用。
下述代码,充分表明了this关键的作用和特点。
1 只可以用this关键字在构造器里调用其它构造器。
2 构造器的调用必须在语句的第一位置。
package thinkJava; public class Flower {
int petalCount = 0;
String s = "initial value"; Flower(int petals) {
petalCount = petals;
System.out.println("Constructor w/ int arg only. petalCount = "
+ petalCount);
} Flower(String ss) {
System.out.println("Constructor w/ String arg only, s = " + ss);
s = ss;
} Flower(String s, int petals) {
this(petals);
// this(s); //Can't call two!
this.s = s; // Another use of "this"
System.out.println("petalCount = " + petalCount + " s = " + s);
} Flower() {
this("hi", 47);
System.out.println("Default constructor(no args)");
} void printPetalCount() {
// this(11); //Not inside non-Constructor
System.out.println("petalCount = " + petalCount + " s = " + s);
} public static void main(String[] args) {
Flower x = new Flower();
x.printPetalCount();
}
}
猜猜输出是什么?
Constructor w/ int arg only. petalCount = 47
petalCount = 47 s = hi
Default constructor(no args)
petalCount = 47 s = hi
变量的初始化顺序
package javaTest; class Windows {
Windows(int marker) {
System.out.println("Windows(" + marker + ")");
}
} class House {
Windows w1 = new Windows(1); House() {
System.out.println("House()");
w3 = new Windows(33);
} Windows w2 = new Windows(2); void f() {
System.out.println("f()");
} Windows w3 = new Windows(3);
} public class OrderOfInitialzation {
public static void main(String[] args) {
House h = new House();
h.f();
}
}
初始化顺序
输出的结果是:
Windows(1)
Windows(2)
Windows(3)
House()
Windows(33)
f()
添加静态变量后的初始化情况
package thinkJava; class Bowl {
Bowl(int maker) {
System.out.println("Bowl(" + maker + ")");
} void f1(int maker) {
System.out.println("f1(" + maker + ")");
}
} class Table {
static Bowl bowl1 = new Bowl(1); Table() {
System.out.println("Table()");
bowl2.f1(1);
} void f2(int marker) {
System.out.println("f2(" + marker + ")");
} static Bowl bowl2 = new Bowl(2);
} class Cupboard {
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4); Cupboard() {
System.out.println("Cupboard()");
bowl4.f1(2);
} void f3(int maker) {
System.out.println("f3(" + maker + ")");
} static Bowl bowl5 = new Bowl(5);
} public class StaticInitialzation {
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
} static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
静态成员的初始化顺序
猜测结果是什么?
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
初始化结果
4.1 static方法的特性
有些人认为static方法不是"面向对象"的,因为它们的确具有全局函数的语义;使用static方法时,
由于不存在this,所以不是通过"向对象发送消息"的方式完成的。的确,要是在代码中出现了大量的static方法,就该重新考虑自己的设计了。
static是可以修饰属性 方法 内部类 自由块,不能修改方法的局部变量。
5 异常抛出
异常可以直接抛出,也可以循环自恢复。
package exceptions; public class Ex5 {
private static int[] ia = new int[2];
private static int x = 5; public static void main(String[] args) {
while (true) {
try {
ia[x] = 1;
System.out.println("ia[" + x + "]= " + ia[x]);
break;
} catch (Exception e) {
x--;
System.err.println("Caught ArrayIndexOutOfBoundsExceptions");
e.printStackTrace();
} finally {
System.out.println("Are we done yet?");
}
}
System.out.println("Now, we are done."); }
}
异常恢复模型
结果是:
自定义异常
package exceptions; class MyExceptions extends Exception { private static final long serialVersionUID = 1L;
private int x; public MyExceptions() {
} public MyExceptions(String msg) {
super(msg);
} public MyExceptions(String msg, int x) {
super(msg);
this.x = x;
} public int val() {
return x;
} public String getMessage() {
return "Detial Message: " + x + " " + super.getMessage();
}
} public class ExtraFeatures {
public static void f() throws MyExceptions {
System.out.println("Throwing MyException from f()");
throw new MyExceptions();
} public static void g() throws MyExceptions {
System.out.println("Throwing MyException from g()");
throw new MyExceptions("Originated in g()");
} public static void h() throws MyExceptions {
System.out.println("Throwing MyException from h()");
throw new MyExceptions("Originated in g()", 47);
} public static void main(String[] args) {
try {
f();
} catch (MyExceptions e) {
e.printStackTrace(System.out);
}
try {
g();
} catch (MyExceptions e) {
e.printStackTrace(System.out);
}
try {
h();
} catch (MyExceptions e) {
e.printStackTrace(System.out);
System.out.println("e.val() = " + e.val());
}
}
}
我的异常
运行的结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgMAAADdCAIAAACpEuZ2AAAgAElEQVR4nO2dQZKrsK6GWZcX5HVkCcxSb3Y3kNktFpAd3EkmLOEsgjcAbMmWhJ1OCOD/q646p2liC9mxbAP6u//85z//99///d9//zcBAABokg6RAAAAGgeRAAAAWgeRAAAAWidGgrF3XYrrx2k+7oevmzL4zvXjp0t9+e7WdfdY8PDoulvnntoHxv7edTf2o5+8J4O/uf7fN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IBAAC0DiIBAAC0zv8D0ffyJVti5W8AAAAASUVORK5CYII=" alt="" />
5.1 异常列表/异常说明
package exceptions; class ExceptionA extends Exception { private static final long serialVersionUID = 1L; public ExceptionA(String msg) {
super(msg);
}
} class ExceptionB extends Exception { private static final long serialVersionUID = 1L; public ExceptionB(String msg) {
super(msg);
}
} class ExceptionC extends Exception { private static final long serialVersionUID = 1L; public ExceptionC(String msg) {
super(msg);
}
} public class Ex9 {
/*
* 异常说明使用了附加关键字throws,后面紧接着一个所有潜在异常类型的列表
*/
public static void f(int x) throws ExceptionA, ExceptionB, ExceptionC {
if (x > 0)
throw new ExceptionA("Exception A");
if (x == 0)
throw new ExceptionB("Exception B");
if (x < 0)
throw new ExceptionC("Exception C");
} public static void main(String[] args) {
try {
f(-1);
f(0);
f(1);
} catch (Exception e) {
System.out.println("Caught Exception");
e.printStackTrace(System.out);
}
}
}
异常说明/throws
输出的结果是
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXgAAABWCAIAAABKLhoDAAAHQ0lEQVR4nO2dy3WkMBBFiUsBKQ6HoF2fCaJ3PgTgDGbjDSFMEMyCj0pCJcBGNKjvPV6Mu0EqFaOHSt08N38+//YAACVpEBoAKA1CAwDFQWgAoDgIDQAUxwtNa5sR4zpnbHtYH50zTdP8vsGhnQDjjgjwt7S2Ma57dRQA12UUmtZ6JThKFyQZ5dqlaZ0zfkp3zpwoNIdqL8B70fz5/JuTAb/O8TdtqUTD++J+Pp9gnLPzW50z1s0LklDSBKtTeRKa1kqJmdqx7dy/j0iu1LxMya6nppzxYQeN6HHO7yxXNOKc8fAxb9YumgGonObP59++terKv23nyeBMMMH8LBGn+2M6Z2JtGn8LViX7VzTpoqlzZmi/c0bM4MVKbew5WhiFWiNPsGL4apzRiIZ2Aqmb2pfRLc8CqJU1oQlv5itCE9YyciJpwtT/sHQKVzRBqLLt1GGLQcmRBdIShr1DaBYrRJkhLQ8AFTOVTunNjmCXU06SLUIjJ9LRQqMEGxckitCor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1rJZ4UsrHH+1VPdQhNoPHRNNyzosEATOkxOn1FrtZLp7kP9a200CR7e67N6vXSSbYmOvq2Qr/Ccmz6NVCB3PEb0OKMX0doXoT8L7tYuGcMk5ZgAJbscdXgSrLYrVT8bnaWTlJo5hWKKIu+bWJbZGhrYwkzHP1M7zpHpdnQjvmKlzP54xf9yvKqtf4DMmf8WDr3CFc0lE6vQxQro3NSsH2wuWToMQD7mcFVrjJLCM3qLr28aSc+3m6erXhdbAZP81Nsyi4+3t4sNEHXD2sf0bppIQH545P9Ths0i+0nvz8db0uxGQxrYOy0lRd9ipur0a4BH2/DChg77eJEUVaXFZfjnsuZHqEBgBNAaODqLDZo3vTn1dfhVyA0AFAchAYAioPQAEBxEBoAKA5CAwDFQWhuQK3GV3v5WR7e2kBLi//07+PULzSXMpTKcKk4zzC+Ooc3N9DS4z/5G8a1CA3GV3cxvhoaMWZ8AGpsZzo7eR2VPOTHO4/6nQ20cvGfu6ipRmgwvppjuLzx1XzElD6vU/p1TOZBG68I670NtHLxn7r0rEVoML66kfHVfC+ddKALfSi0QJNCk/PfwUArG/+ZvjZ1CA3GV7cyvlKFJncdk1H9VGiSUWOgVZAqhAbjq3sZX2lCk72OyTysCeu7G2hl46d02g/GV302zgsZX8lWWju00E17V8p1TOchf13mq/DWBlqZ+NkMrhiMr84GAy0FPt6uFoyvXgIGWgn4wh4A1AdCA+/Cy52rLvLzmuQjNABQGoQGAIqD0ABAcRAaACgOQgMAxUFoAKA4CA0AFAehAYDiIDQAUByEBgCKg9AAQHEQGgAoDkIDAMVBaACgOAgNABQHoQGA4iA0AFAchAYAivMfY4HeSHuSnCQAAAAASUVORK5CYII=" alt="" />
6 继承
构造器的初始化
package reusing;
/*
* 对于基类的正确初始化是至关重要的。
* 在构造器中调用基类构造器来执行初始化。
* 基类构造器具有执行基类初始化方法所需要的知识和能力。
* 导出类构造器会自动调用基类的默认构造器,但是如果基类明确定义了带参数的构造器,
* 就必须在导出类构造器中明确的调用。
*/ class Game{
Game(int i){
System.out.println("Game Constructor");
}
}
class BoardGame extends Game{
BoardGame(int i) {
super(i);
System.out.println("BoardGame Constructor");
}
}
public class Chess extends BoardGame{
Chess(int i){
super(i);
System.out.println("Chess Constructor");
}
public static void main(String[] args){
new Chess(1);
}
}
构造器初始化
运行结果
Game Constructor
BoardGame Constructor
Chess Constructor
result
手动清理以及清理顺序例子
package reusing;
class Shape{
Shape(int i){
System.out.println("Shape Constructor.");
}
void dispose(){
System.out.println("Shape dispose.");
}
} class Circle extends Shape{
Circle(int i) {
super(i);
System.out.println("Drawing Circle");
}
void dispose(){
System.out.println("Erasing Circle");
super.dispose();
}
} class Triangle extends Shape{
Triangle(int i) {
super(i);
System.out.println("Drawing Triangle");
}
void dispose(){
System.out.println("Erasing Triangle");
super.dispose();
}
}
class Line extends Shape{
private int start, end;
Line(int start, int end){
super(start);
this.start = start;
this.end = end;
System.out.println("Drawing Line");
}
void dispose(){
System.out.println("Erasing Line" + start + ", " + end);
super.dispose();
}
}
public class CADSystem extends Shape{
private Circle c;
private Triangle t;
private Line[] lines = new Line[3];
public CADSystem(int i){
super(i + 1);
for(int j = 0; j < lines.length; j++){
lines[j] = new Line(j, j*j);
}
c = new Circle(1);
t = new Triangle(1);
System.out.println("Combined constructor");
}
public void dispose(){
System.out.println("CADSystem.dispose()");
//The order of cleanup is the reverse
//of the order of initialization;
t.dispose();
c.dispose();
for(int i = lines.length - 1; i >= 0; i--){
lines[i].dispose();
}
} public static void main(String[] args){
CADSystem x = new CADSystem(47);
try{ }finally{
x.dispose();
}
} /*
* Shape Constructor.
* Shape Constructor.
* Drawing Line
* Shape Constructor.
* Drawing Line
* Shape Constructor.
* Drawing Line
* Shape Constructor.
* Drawing Circle
* Shape Constructor.
* Drawing Triangle
* Combined constructor
* CADSystem.dispose()
* Erasing Triangle
* Shape dispose.
* Erasing Circle
* Shape dispose.
* Erasing Line2,4
* Shape dispose.
* Erasing Line1,1
* Shape dispose.
* Erasing Line0,0
* Shape dispose.
*
*/ }
清理代码
7 多态
私有方法有多态吗?
package ploymorphism; class Derived extends PrivateOverride{
public void f(){
System.out.println("public f()");
}
} /*
* 多态不能覆盖父类的私有方法,尽量避免子类和父类的私有方法名字一致,从而避免引起混淆。
* 下面的代码输出是什么?把f()的权限公开,结果又怎样?
*/
public class PrivateOverride {
private void f(){
System.out.println("private f()");
}
public static void main(String[] args){
PrivateOverride po = new Derived();
po.f();
}
} /*
* private f()
*/
PrivateOverride
公开域有多态吗?
package ploymorphism; class Super {
public int field = 0; public int getField() {
return field;
}
} class Sub extends Super {
public int field = 1; public int getField() {
return field;
} public int getSuperField() {
return super.field;
}
} public class FieldAccess {
public static void main(String[] args) {
Super sup = new Sub(); // Up casting
System.out.println("sup.field = " + sup.field + ", sup.getField() = "
+ sup.getField());
Sub sub = new Sub();
System.out.println("sub.field = " + sub.field + ", sub.getField() = "
+ sub.getField() + ", sub.getSuperField = "
+ sub.getSuperField());
} } /*
* 任何域的访问操作都将有编译器解析,因此不是多态的。
* Sub实际上有两个field的域。它自己的和从Super处得到的。
* 引用Sub中的域field时所产生的默认域并非Super版本的域。
*
*/ /*
* Output:
* sup.field = 0, sup.getField() = 1
* sub.field = 1, sub.getField() = 1, sub.getSuperField = 0
*
*/
FieldAccess
静态方法有多态吗?
package ploymorphism;
class StaticSuper{
public static String staticGet(){
return "Base staticGet";
}
public String dynamicGet(){
return "Base dynamicGet()";
}
}
class StaitcSub extends StaticSuper{
public static String staticGet(){
return "Derived staticGet()";
}
public String dynamicGet(){
return "Derived dynamicGet()";
}
}
public class StaticPolymorphism {
public static void main(String[] args){
StaticSuper sup = new StaitcSub();
System.out.println(sup.staticGet());
System.out.println(sup.dynamicGet());
}
} /*
* 如果某个方法是静态的,它的行为就不具有多态性。
* 静态方法是与类,而非单个对象相关联的。
*
* Outputs:
*
* Base staticGet
* Derived dynamicGet()
*/
StaticPolymorphism
只有普通的公开方法有多态的特性。
含有构造器,继承,静态域的初始化顺序
package ploymorphism; class Meal {
Meal() {
System.out.println("Meal()");
}
} class Bread {
Bread() {
System.out.println("Bread()");
}
} class Chess {
Chess() {
System.out.println("Chess()");
}
} class Lettuce {
Lettuce() {
System.out.println("Lettuce()");
}
} class Lunch extends Meal {
Lunch() {
System.out.println("Lunch()");
}
} class ProtableLunch extends Lunch {
ProtableLunch() {
System.out.println("ProtableLunch()");
}
} public class SandWitch extends ProtableLunch{ private Bread b = new Bread();
private Chess c = new Chess();
private Lettuce l = new Lettuce(); private static ProtableLunch pp = new ProtableLunch();//
private static Lunch ll = new Lunch();// public SandWitch() {
System.out.println("SandWitch()");
} public static void main(String[] args) {
new SandWitch();//
}
} /*
* 初始化顺序,调用基类构造器
* 按声明顺序调用成员初始化的方法
* 调用导出类构造器的主体
* 先静态域,静态方法,数据域,构造器,普通方法
*
*/ /*
* Outputs:
*
* Meal()
* Lunch()
* ProtableLunch()
* Meal()
* Lunch()
* Meal()
* Lunch()
* ProtableLunch()
* Bread()
* Chess()
* Lettuce()
* SandWitch()
*
*
*/
初始化顺序
8 数组
类似于C系列的函数求解
package arrays; import java.util.Arrays;
import java.util.Random; public class IceCream {
private static Random rand = new Random(47);
static final String[] FLAVORS = { "Chocolate", "Strawberry",
"Vanilla Fudge Swirl", "Mint Chip", "Mocha Almond Fudge",
"Rum Raisin", "Praline Cream", "Mud Pie", }; public static String[] flavorSet(int n) {
if (n > FLAVORS.length) {
throw new IllegalArgumentException("Set to big");
}
String[] results = new String[n];
boolean[] picked = new boolean[FLAVORS.length];
for (int i = 0; i < n; i++) {
int t;
do {
t = rand.nextInt(FLAVORS.length);
} while (picked[t]);
results[i] = FLAVORS[t];
picked[t] = true;
}
return results;
} public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
System.out.println(Arrays.toString(flavorSet(4)));
}
}
} /*
* OutPuts:
*
* [Rum Raisin, Mint Chip, Mocha Almond Fudge, Chocolate]
* [Strawberry, MochaAlmond Fudge, Mint Chip, Rum Raisin]
* [Vanilla Fudge Swirl, Mud Pie,Chocolate, Mocha Almond Fudge]
* [Praline Cream, Strawberry, Mocha AlmondFudge, Mint Chip]
* [Mint Chip, Strawberry, Praline Cream, Chocolate]
* [Chocolate, Praline Cream, Mocha Almond Fudge, Mint Chip]
* [Mud Pie,Strawberry, Mint Chip, Rum Raisin]
*
*/
数组随机组
9 泛型
Java中的泛型需要与C++进行一番比较,理由有二:首先,了解C++模板的默写方面,有助于理解泛型的基础。
同时,也可以了解Java的局限,以及为什么有这些限制。最终帮你理解,Java泛型的边界在哪里。理解了边界所在,
才能成为程序高手(不必浪费时间在死胡同里乱转)。第二个原因是,在Java社区中,人们普遍对C++模板有一种误解,
而这种误解可能误导你,令你在理解泛型的意图时产生偏差。
10 内部类
package innerclasses;
/*
* 接口就相当于声明语句
*/
interface Selector {
public int dex = 0;
boolean end(); Object current(); void next();
} public class Sequence {
private static Object[] item;
private int next; public Sequence(int size) {
item = new Object[size];
} public void add(Object x) {
if (next < item.length) {
item[next++] = x;
}
} /*
* 内部类可以访问外围类的字段和方法, 加static关键字表示嵌套类(静态内部类)
*/
public static class SequenceSelector implements Selector {
private int i = 0; public boolean end() {
return i == item.length;
} public Object current() {
return item[i];
} public void next() {
if (i < item.length) {
i++;
}
}
} public Selector selector() {
return new SequenceSelector();
} public static void main(String[] args) {
Sequence sequence = new Sequence(10);
for (int i = 0; i < 10; i++) {
sequence.add(Integer.toString(i));
}
Selector selector = sequence.selector();
while (!selector.end()) {
System.out.print(selector.current() + " ");
selector.next();
}
} } class ABC {
/*
* 先实例化外围类
*/
Sequence aSequence = new Sequence(5);
/*
* 实例化的外围类生成构造器
*/
Sequence.SequenceSelector aSelector = new Sequence.SequenceSelector();
int x = Selector.dex;
}
内部类接口实现
11 容器、迭代器
迭代器能够将遍历的操作与序列底层的结构分离。正由于此,我们有时会说;迭代器统一了对容器的访问方式。
JAVA语言学习笔记(一)的更多相关文章
- java语言学习笔记1
最近也在春节的节假日里,总是会有各种各样的事情出现,没有很完整的时间来学习java以及其他方面的知识. 从昨天开始有了完整的学习体系,我去娄老师推荐的极客学院网站开始学习java语言的语法.在假期之初 ...
- 20145213《Java程序设计学习笔记》第六周学习总结
20145213<Java程序设计学习笔记>第六周学习总结 说在前面的话 上篇博客中娄老师指出我因为数据结构基础薄弱,才导致对第九章内容浅尝遏止地认知.在这里我还要自我批评一下,其实我事后 ...
- [原创]java WEB学习笔记75:Struts2 学习之路-- 总结 和 目录
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 2017-04-21周C语言学习笔记
C语言学习笔记:... --------------------------------- C语言学习笔记:学习程度的高低取决于.自学能力的高低.有的时候生活就是这样的.聪明的人有时候需要.用笨的方法 ...
- 2017-05-4-C语言学习笔记
C语言学习笔记... ------------------------------------ Hello C语言:什么是程序:程序是指:完成某件事的既定方式和过程.计算机中的程序是指:为了让计算机执 ...
- Java Web 学习笔记 1
Java Web 学习笔记 1 一.Web开发基础 1-1 Java Web 应用开发概述 1.1.1 C/S C/S(Client/Server)服务器通常采用高性能的PC机或工作站,并采用大型数据 ...
- Go语言学习笔记十三: Map集合
Go语言学习笔记十三: Map集合 Map在每种语言中基本都有,Java中是属于集合类Map,其包括HashMap, TreeMap等.而Python语言直接就属于一种类型,写法上比Java还简单. ...
- Go语言学习笔记十一: 切片(slice)
Go语言学习笔记十一: 切片(slice) 切片这个概念我是从python语言中学到的,当时感觉这个东西真的比较好用.不像java语言写起来就比较繁琐.不过我觉得未来java语法也会支持的. 定义切片 ...
- Go语言学习笔记十: 结构体
Go语言学习笔记十: 结构体 Go语言的结构体语法和C语言类似.而结构体这个概念就类似高级语言Java中的类. 结构体定义 结构体有两个关键字type和struct,中间夹着一个结构体名称.大括号里面 ...
随机推荐
- Jquery打叉怎么办
选中报错文件右键MyEclipse>Exclude From xxxx
- 【poj1085】 Triangle War
http://poj.org/problem?id=1085 (题目链接) 题意 A,B两人玩游戏,在一个大三角形上放火柴,若A放上一根火柴后成功组成一个三角形,那么这个三角形就归属于A,并且A被奖励 ...
- text-indent无效解决方案
text-indent是用来字符缩进的. 1.text-indent所在的元素是行内元素而非块级元素.比如用在span,a等行内元素上.解决方案:在行内元素加上display:block; 或者把目标 ...
- phpcms /api/phpsso.php SQL Injection Vul
catalogue . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Relevant Link:2. 漏洞触发条件3. 漏洞影响范围4. ...
- redis安装,配置
下载 cd /usr/local/src wget http://download.redis.io/releases/redis-3.0.7.tar.gz 安装 yum install je ...
- HDU 4757 Tree
传送门 Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Prob ...
- python使用cookielib库示例分享
Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持,下面是使用示例 该模块主要功能是提供可存储cookie的对象.使用此模块捕获 ...
- Visual Studio 2012环境变量、工作目录、vc++目录、 命令等 的配置和作用
在调试 Visual Studio 212 程序时,经常有一些动态链接库(即 dll 文件)需要加载到工程里,这样才能依赖第三方库进行程序调试. 这些动态链接库,往往都是测试版本或是开发中的版本,或者 ...
- python标准模块(一)
本文会涉及到的模块: time datetime sys os random re hashlib 模块,用若干代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能 ...
- COGS1117
传送门: 差分约束第一题. 所有的条件无非两种不等式 $d[i]-d[j]>=dist$ $d[i]-d[j]<=dist$ 然后进行变形 $d[i]-d[j]>=dist$ ...