This commit is contained in:
cyp0633 2021-12-19 20:23:59 +08:00
commit 3aeb6f7d84
18 changed files with 300 additions and 14 deletions

View File

@ -2,12 +2,20 @@
包含Thinking in Java书中代码和老师课上添加代码
可导入IntelliJ IDEA包含依赖库
可导入IntelliJ IDEA包含依赖库不确定是否适用于Eclipse
基于[xbtlin/thinking-in-Java](https://github.com/xbtlin/thinking-In-Java)
fork自[xbtlin/thinking-in-Java](https://github.com/xbtlin/thinking-In-Java)
**部分冲突的类或文件难以通过改包解决,已将后缀改为.bak**
## 食用方法
## IDEA食用方法
IntelliJ IDEA-文件-新建-来自版本控制的项目在URL中输入https://github.com/cyp0633/object-oriented-programming
**前置条件**你需要能够顺畅访问GitHub的网络条件当你看到这条消息的时候就已经满足了
1. IntelliJ IDEA-文件-新建-来自版本控制的项目在URL中输入https://github.com/cyp0633/object-oriented-programming
2. 点击信任项目
3. 打开“文件-项目结构”将“项目SDK”设为任何一个1.8.0版本(没有装的可以在 [清华大学开源镜像站](https://mirrors.tuna.tsinghua.edu.cn/#) 下载)
4. 切换到模块-依赖,点加号-JAR或目录导入项目根目录的三个JAR文件
5. 尝试构建项目如果成功则理论上可以使用了否则请提issue
如果你的IDEA还不是中文请1自己对照英语寻找或2安装中文插件

View File

@ -19,6 +19,14 @@
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations-java5</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>

View File

@ -0,0 +1,16 @@
package callback;
public class Display implements MyCallInterface {
private int temperature;
public void method(int temp)
{
this.temperature=temp;
System.out.println("temperature:"+this.temperature);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Heater call = new Heater();
call.setCallfuc(new Display()); //加入观察者
call.start();
}
}

View File

@ -0,0 +1,25 @@
package callback;
public class Heater {
public MyCallInterface mc;
public void setCallfuc(MyCallInterface mc){
this.mc= mc;
}
public void start(){
int temp;
try
{
for(int i=0;i<100;i++){
temp=i;
this.mc.method(temp);
Thread.sleep(2000);
}
}
catch(Exception e)
{
}
}
}

View File

@ -0,0 +1,7 @@
package callback;
public interface MyCallInterface
{
public void method(int temp);
}

View File

@ -0,0 +1,12 @@
package callback;
public class TestMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
Heater call = new Heater();
call.setCallfuc(new Display()); //加入观察者
call.start();
}
}

View File

@ -0,0 +1,23 @@
package exceptions;
class OneException1 extends Exception{}
class TwoException1 extends Exception{}
class a1{
public a1() throws OneException1{
}
public void event() throws TwoException1{}
}
public class MyETest1 extends a1{
public MyETest1() throws TwoException1, OneException1 {
super();
}
//public void event()throws OneException1 {} //编译失败
//public void event()throws OneException1,TwoException1 {}//编译失败
public void event()throws TwoException1 {}
}

View File

@ -0,0 +1,28 @@
package exceptions;
class OneException2 extends Exception{}
class TwoException2 extends Exception{}
class ThreeException2 extends Exception{}
class a2{
public a2() throws OneException2{ }
public void event() throws TwoException2{}
}
interface B{
public void event()throws ThreeException2;
public void show() throws ThreeException2;
}
public class MyETest2 extends a2 implements B{
public MyETest2() throws TwoException2, OneException2 {
super();
}
// public void event()throws ThreeException2 {} //编译失败
//public void event()throws TwoException2,ThreeException2 {} //编译失败
//public void event()throws TwoException2 {}//编译失败
public void event(){ }
public void show() throws ThreeException2 { }
}

View File

@ -0,0 +1,23 @@
package exceptions;
class OneException3 extends Exception{}
class TwoException3 extends Exception{}
class ThreeException3 extends TwoException3{}
class a3{
public a3() throws OneException3{
}
public void event() throws TwoException3{}
}
public class MyETest3 extends a3 {
public MyETest3() throws TwoException3, OneException3 {
super();
}
public void event() throws ThreeException3{
}
}

View File

@ -0,0 +1,32 @@
package innerclasses;
import innerclasses.MultiInterfaces1.A1;
import innerclasses.MultiInterfaces1.B1;
public class MultiInterfaces1 {
interface A1 {}
interface B1 {}
static void takesA(A1 a){}
static void takesB(B1 b){}
public static void main(String[] args) {
X1 x = new X1();
Y1 y = new Y1();
//1
takesA(x);
takesA(y);
//2
takesB(x);
takesB(y.makeB());
}
}
class X1 implements A1, B1 {}
class Y1 implements A1 {
B1 makeB() {
return new B1(){};
}
}

View File

@ -1,11 +1,11 @@
//: innerclasses/Parcel10.java
package innerclasses; /* Added by Eclipse.py */
package innerclasses; /* Added by Eclipse.py */
// Using "instance initialization" to perform
// construction on an anonymous inner class.
public class Parcel10 {
public Destination
destination(final String dest, final float price) {
destination(final String dest, final float price) {//final might be omitted
return new Destination() {
private int cost;
// Instance initialization for each object:

View File

@ -1,5 +1,5 @@
//: innerclasses/Sequence.java
package innerclasses; /* Added by Eclipse.py */
package innerclasses; /* Added by Eclipse.py */
// Holds a sequence of Objects.
interface Selector {

View File

@ -1,5 +1,5 @@
//: interfaces/Adventure.java
package interfaces; /* Added by Eclipse.py */
package interfaces; /* Added by Eclipse.py */
// Multiple interfaces.
interface CanFight {
@ -15,13 +15,19 @@ interface CanFly {
}
class ActionCharacter {
public void fight() {}
public void fight() {
System.out.println("ActionCharacter:fight()");
}
}
class Hero extends ActionCharacter
implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
public void swim() {
System.out.println("Hero:swim()");
}
public void fly() {
System.out.println("Hero:fly()");
}
}
public class Adventure {

View File

@ -77,11 +77,11 @@ public class NestingInterfaces {
public static void main(String[] args) {
A a = new A();
// Can't access A.D:
//! A.D ad = a.getD();
// A.D ad = a.getD();
// Doesn't return anything but A.D:
//! A.DImp2 di2 = a.getD();
// A.DImp2 di2 = a.getD();
// Cannot access a member of the interface:
//! a.getD().f();
// a.getD().f();
// Only another A can do anything with getD():
A a2 = new A();
a2.receiveD(a.getD());

View File

@ -0,0 +1,20 @@
package observesample;
import java.util.Observable;
import java.util.Observer;
public class Alarm implements Observer {
@Override
public void update(Observable arg0, Object arg1) {
this.makeAlarm((Integer) arg1);
}
private void makeAlarm(int temperature) {
System.out.println("鍢€鍢€鍢€銆傘€傘€傛按宸茬粡鐑у紑浜?鐜板湪姘存俯鏄細 " + temperature);
}
}

View File

@ -0,0 +1,33 @@
package observesample;
import java.util.Observable;
import java.util.Observer;
public class Display extends Observable implements Observer {
private String status = "鏈紑";
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public void update(Observable o, Object arg) {
this.displayTemperature(((Heater) o).getTemperature());
}
private void displayTemperature(int temperature) {
if (temperature > 100) {
this.setStatus("娌歌吘");
this.setChanged();
this.notifyObservers(temperature);
}
System.out.println("鐘舵€侊細 " + status + " 鐜板湪娓╁害锛?" + temperature);
}
}

View File

@ -0,0 +1,24 @@
package observesample;
import java.util.Observable;
public class Heater extends Observable {
private int temperature;
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public void boilWater() {
for (int i = 90; i < 110; i++) {
temperature = i;
this.setChanged();
this.notifyObservers();
}
}
}

View File

@ -0,0 +1,21 @@
package observesample;
public class TestMain {
public static void main(String[] args) {
Heater heater = new Heater();
Display display = new Display();
Alarm alarm = new Alarm();
heater.addObserver(display);
display.addObserver(alarm);
heater.boilWater();
}
}