1、接口:
结果
5、int、byte、short
6、接口继承接口
接口是特殊的抽象类
接口的定义格式:abstract通常不写
public abstract interface 接口名{}
2、内部类:
(1)匿名内部类:匿名子类的类名
(2)成员内部类:跟方法同级别的类,定义在一个类里面
访问权限可以是四种访问权限的任意一种
构造对象方式:
a)非static:外部类.内部类 对象名 = 外部类对象.new 内部类()
b)Static:外部类.内部类 对象名 = new 外部类.内部类()
(3)方法内部类;直接在方法内部定义声明的类
注意:一定不能有访问权限
注意:同一个文件的多个并列类,不是内部类结构
条件:只能有一个为public或者都是默认不写;如果有public的类,则这个类的类名必须和文件名一样
3、String
package com.test.exercise2;
public class StringTest {
public static void main(String[] args) {
String str = "ok,yes";
char[] chars = { 'a', 'b', 'c' };
new StringTest().change(str, chars);
System.out.println(str);
System.out.println(chars[0]);
}
public void change(String str, char[] chars) {
str = new String();//创建一个新的空间
str = "change";//新的空间里的值改成change
chars[0] = 's';
}
}
结果
原因:
4、switch
switch(只能是String或者int),jdk1.7以后才能使用String
package com.test.exercise2;
public class SwitchTest {
public static void main(String[] args) {
String str = "aa";
switch (str) {
case "aa":
System.out.println("我是”aa“");
break;
case "bb":
System.out.println("我是“bb”");
break;
}
}
}
package com.test.exercise2;
public class SwitchTest {
public static void main(String[] args) {
int value1 = 1;
switch (value1) {
case 1:
System.out.println("我是“1”");
break;
case 2:
System.out.println("我是“2”");
break;
}
}
}
package com.test.exercise2;
public class SwitchTest {
public static void main(String[] args) {
short value1 = 1;
switch (value1) {// 自动转型为int
case 1:
System.out.println("我是short“1”");
break;
case 2:
System.out.println("我是short“2”");
break;
}
}
}
package com.test.exercise2;
public class SwitchTest {
public static void main(String[] args) {
byte value1 = 1;
switch (value1) {// 自动转型为int
case 1:
System.out.println("我是byte“1”");
break;
case 2:
System.out.println("我是byte“2”");
break;
}
}
}
5、int、byte、short
byte value = 1;
value=value+1;//会报错,byte类型+int类型结果是int类型
short value = 1;
value = value + 1;// 会报错,short类型+int类型结果是int类型
但是:
short value = 1;
value += 1;// 不会报错
byte value = 1;
value += 1;// 不会报错
6、接口继承接口
package com.test.exercise2;
public interface InterfaceTest {
}
package com.test.exercise2;
public interface InterfaceTest3 {
}
package com.test.exercise2;
public interface InterfaceTest2 extends InterfaceTest, InterfaceTest3 {// 没有错!!!
}