forked from javastacks/javastack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John
committed
Jan 21, 2020
1 parent
3e8484e
commit fce157e
Showing
21 changed files
with
1,286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
![image](http://img.javastack.cn/17-12-20/25802617.jpg) | ||
|
||
最近,微信群友在讨论子类父类的转换问题,其实不难,给大家用实例来说明一下就很明了了。 | ||
|
||
我们知道Java中子类转换成父类是没有任何问题的,那父类可以转换成子类吗? | ||
|
||
来看下面这段程序: | ||
|
||
``` | ||
public class TestObjectConvert { | ||
public static void main(String[] args) { | ||
test1(); | ||
test2(); | ||
} | ||
private static void test1() { | ||
Fruit fruit1 = new Fruit(); | ||
Apple apple1 = new Apple(); | ||
apple1 = (Apple) fruit1; // java.lang.ClassCastException | ||
} | ||
private static void test2() { | ||
Fruit fruit1 = new Apple(); | ||
Apple apple1 = new Apple(); | ||
apple1 = (Apple) fruit1; | ||
} | ||
static class Fruit { | ||
} | ||
static class Apple extends Fruit { | ||
} | ||
} | ||
``` | ||
|
||
结果是: | ||
|
||
``` | ||
test1:报类转异常; | ||
test2:转换正常。 | ||
``` | ||
|
||
**所以,想让父类强制转换成子类,不是没有可能,除非父类是子类构造出来的实例,不然是不能强转的。** | ||
|
||
为什么呢? | ||
|
||
如上代码,如果父类实例出来的对象是Orange,Orange当然不能强制转成Apple,所以说父类只有该子类对应的实例才能强转。 | ||
|
||
在公众号回复"wx"加入微信群,可参与更多技术话题讨论。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
我们都知道 `switch` 用来走流程分支,大多情况下用来匹配单个值,如下面的例子所示: | ||
|
||
``` | ||
/** | ||
* @from 微信公众号:Java技术栈 | ||
* @author 栈长 | ||
*/ | ||
private static void test(int value) { | ||
switch (value) { | ||
case 1: | ||
System.out.println("1"); | ||
break; | ||
case 2: | ||
System.out.println("1"); | ||
break; | ||
case 3: | ||
System.out.println("1"); | ||
break; | ||
case 4: | ||
System.out.println("1"); | ||
break; | ||
case 5: | ||
System.out.println("1"); | ||
break; | ||
case 6: | ||
System.out.println("0"); | ||
break; | ||
case 7: | ||
System.out.println("0"); | ||
break; | ||
default: | ||
System.out.println("-1"); | ||
} | ||
} | ||
``` | ||
|
||
相关阅读:[switch case 支持的 6 种数据类型](https://mp.weixin.qq.com/s/QuchavZfEexwAgUS5qgB_Q)。 | ||
|
||
大概的意思就是,周一到周五输出:1,周六到周日输出:0,默认输出-1。 | ||
|
||
这样写,很多重复的逻辑,冗余了。 | ||
|
||
也许这个例子不是很合适,用 if/ else 更恰当,但这只是个例子,实际开发中肯定会有某几个 case 匹配同一段逻辑的情况。 | ||
|
||
那么,如何让多个 case 匹配同一段逻辑呢? | ||
|
||
如下面例子所示: | ||
|
||
``` | ||
/** | ||
* @from 微信公众号:Java技术栈 | ||
* @author 栈长 | ||
*/ | ||
private static void test(int value) { | ||
switch (value) { | ||
case 1: case 2: case 3: case 4: case 5: | ||
System.out.println("1"); | ||
break; | ||
case 6: case 7: | ||
System.out.println("0"); | ||
break; | ||
default: | ||
System.out.println("-1"); | ||
} | ||
} | ||
``` | ||
|
||
把相同逻辑的 case 放一起,最后一个 case 写逻辑就行了。 | ||
|
||
格式化后就是这样了: | ||
|
||
``` | ||
/** | ||
* @from 微信公众号:Java技术栈 | ||
* @author 栈长 | ||
*/ | ||
private static void test(int value) { | ||
switch (value) { | ||
case 1: | ||
case 2: | ||
case 3: | ||
case 4: | ||
case 5: | ||
System.out.println("1"); | ||
break; | ||
case 6: | ||
case 7: | ||
System.out.println("0"); | ||
break; | ||
default: | ||
System.out.println("-1"); | ||
} | ||
} | ||
``` | ||
|
||
是不是很骚? | ||
|
||
其实这不是最合适的最好的写法,在 Java 12 中还可以更骚。 | ||
|
||
在 Java 12 中可以用逗号来分开多个值,还能用 `lambda` 表达式,甚至还能省略 break,使用 `switch` 更方便,具体看这篇文章:[Java 12 骚操作, switch居然还能这样玩](https://mp.weixin.qq.com/s/EY-2gqbbynshCshRlM3Qsw),或者关注微信公众号:Java技术栈,在后台回复 "新特性" 获取这篇文章。 | ||
|
Oops, something went wrong.