1 Wrapper class
OverLoading 名相同,参数不同,返回类型不要求
1. //public void println(double d){}
//public void println(boolean b){}
public void println(Object o){}0.
2. int num=3;
double d=num;
Integer it=new Integer(3);
Double dd=it;
基本类型数据int可以向double自动扩展,但是包装类型之间
不能自动的相互转换
public class TestWrapper2{
public static void main(String[] args){
// int ------ Integer
int i1=3;
Integer it1=new Integer(i1);
Integer it2=Integer.valueOf(i1);
int i2=it1.intValue();
// Integer -------- String
Integer it3=new Integer(5);
String str1=it3.toString();
Integer it4=Integer.valueOf(str1);
Integer it5=Integer.getInteger(str1);
Integer it6=new Integer(str1);
// int ----------- String
String str2="1234";
int i4=Integer.parseInt(str2); //********
String str3=Integer.toString(i4);
String str4=i4+"";
}
}
Equals方法覆盖
public class TestToString {
public static void main(String[] args) {
String str1 = new String("abc");
//String str = "Hello";
String str2 = new String("abc");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
//System.out.println(str1.toString());
//System.out.println(str1);
Student s1 = new Student("zhangsan" , 18);
Student s2 = new Student("zhangsan", 18);
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
//System.out.println(s.toString());
//System.out.println(s);
}
}
class Student{
String name;
int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
//设置toString方法覆盖,不然父类的方法的方法是打印地址。
public String toString() {
return "Name is " + name + " Age is " + age;
}
//设置equals方法的判断条件
public boolean equals(Object o) {
//标准的5步
if(o == null) return false;//1.是空就返回false
if(o == this) return true;//2.是自己就返回true
//if(!(o instanceof Student)) return false;//可以先这样写,标准写法是下面的
if(!(o.getClass() == this.getClass())) return false;//3.不是是同一种类就返回false
Student s = (Student)o;//4.强转
//判断名字和年龄都相等才相等
return (this.name.equals(s.name)) && (this.age == s.age);//5.根据具体数据判断
//return this.name.equals(s.name);//认为名字相等就相等//不是递归调用
//return this.age == s.age;//认为年龄相等就相等
/***************************************
*** Student s = (Student) o;
*** if(this.age == s.age) return true;
*** else return false;
***************************************/
}
}
--------------------------------------------------------------------------------------------------------
Private 方法,属性 本类
Default 方法,属性,类 在包
Protected 方法,属性 本包和子类
public 全部 到处可见
内部类:定义在其他代码块(类体或者方法体)里的类称为内部类。
编译后每一个内部类都会有自己的独立的字节码文件,文件名Outer$inner.java。
根据位置和修饰符的不同分为四种
1 member inner class 成员内部类
2 static inner class 静态内部类 (嵌套内部类)
类以内,方法之外,有静态修饰(static 可以 修辞内部类)
3 local inner class 局部内部类
方法里的类
4 annonymous inner class 匿名内部类
没有名字的类,只有这种没有构造方法。
Static inner class
1 定义的位置:类以内,方法之外,有静态修饰(static 可以 修辞内部类)
2 本身能定义的属性和功能:所有的属性和功能,包括静态和非静态
3 能直接访问什么:只能访问外部类的静态的功能和属性
4 怎么创建对象:outer.inner in=new outer.inner()
Member inner class 当作成员变量
1 定义的位置:类以内,方法之外
2 本身能定义的属性和功能:只能定义非静态的属性和方法
3 能直接访问什么:能访问外部类的所有的属性和功能
4 怎么创建对象:
Outer.Inner in1=new outer().new Inner()
Outer.Inner in2=outer.new Inner();
在Outer里提供一个getInner(),返回内部类的对象。
只要是类与接口就有字节码文件。
Local inner class 当作局部变量
1 定义的位置:方法之内,不能用public和static
2 本身能定义的属性和功能:只能定义非静态的属性和功能
3 能直接访问什么:能访问外部类的静态的功能和属性,以及方法的用final修饰的局部变量
4 怎么创建对象:相当于局部变量,只能在方法里创建对象。
Annonymous inner class
1、个特殊的局部内部类,没有名字,没有构造方法,可以实现一个接口或者继承一个类,生命周期里只能产生一个对象。
2、除了没有名字外,能定义的和能访问的都和普通的局部内部类一致。
3、当你试图创建接口或者抽象类对象的时候,用匿名内部类表示类本的{}紧跟在构造方法之后,调用匿名内部类的方法只能用写类时创建的那个对象。
public class T1{
public static void main(String[] args){
T1 t1=new T1();
/*int i=Integer.parseInt(args[0]);
Teacher t=t1.getTeacher(i);
t.teach(); */
t1..set(new Teacher(){
public void teach(){
System.out.println("CoreJava");
}
} , 3);
}
public void set(Teacher t,int num){
t.teach();
System.out.println(num);
}
public Teacher getTeacher(int num){
class George implements Teacher{
public void teach(){
System.out.println("EJB");
}
}
if(num==1) return new Teacher(){
public void teach(){
System.out.println("CoreJava");
}
};
else if(num==2) return new Teacher(){
public void teach(){
System.out.println("Web");
}
};
else return new Teacher(){
public void teach(){
System.out.println("EJB");
}
};
}
}
interface Teacher{
void teach();
}
class Outer{
public void get(){
Teacher t=new Teacher(){
public void teach(){ }
};
t.teach();
}
}
class A implements Teacher{
public void teach(){}
}
内部类也是类,也能继承和实现接口。
内部类的作用:
1. 不破坏访问权限的情况下,可以使用外部类的私有成员变量和方法。
2. 将接口公开,将实现类作成内部类隐藏起来,强制要求使用者使用接口,强制降低偶合度。
3. java通过接口和内部类两种机制来实现多继承。
关键字
Abstract do implements private throw boolean double import protected throws break else imstanceof public transient byte
Extends int return true case false interface short
Try catch final long static void char finally
Native super volatile class float new switch while
Continue for null synchronized default if package this goto const