/** 多层嵌套内部类, 调用时要层层往下调用 格式: 外部类.内部类1.内部类2 对象名 = new 外部类().new 内部类1().new 内部类2(); 对象名.属性/方法名(); */ class Outer { public void oSay() { System.out.println("这是 outer 方法"); } public class Inter1 { public void iSay() { System.out.println("这是 inter1 方法"); } public class Inter2 { public void say() { System.out.println("这是 inter2 的 say method"); } } } } public class Hi { public static void main(String[] args) { Outer.Inter1.Inter2 oii = new Outer().new Inter1().new Inter2(); oii.say(); } } // ============= /** 实例内部类 Inter与外部类 Outer有着同名的成员变量i,则在内部类 Inter中, i,this.i和 Inter.this.i都表示为 Inter类的成员i;而 Outer.this.i才表示外部类 Outer 的成员 */ class Outer { int i = 10; public class Inter { int i = 20; public void getOuterI() { System.out.println("i表示为 Inter中的 i 值: "+i); // 20 System.out.println("this.i表示为 Inter中的 i 值;"+this.i); // 20 System.out.println("Inter.this.i表示为 Inter 中 i 的值:"+Inter.this.i); // 20 System.out.println("Outer.this.i表示为 Outer中的 i 值:"+Outer.this.i); // 10 } } } public class Hi { public static void main(String[] args) { Outer.Inter oi = new Outer().new Inter(); oi.getOuterI(); } } // ============= /** 静态内部类创建对象实例 外部类.静态内部类 静态内部类对象 = new 外部类.静态内部类(); */ class Outer { private int a = 4; // 定义实例属性 a private static int b = 5; // 定义静态属性 b public static class Inter // 定义静态内部类 { private int x = 100; // 定义实例属性 x private static int y = 200; // 定义静态属性 y public void add() // 定义实例方法 { // 通过外部类的实例对象调用外部类的非静态成员 int temp = new Outer().a; System.out.println(temp+"+"+x+"="+(x+temp)); } public static void mul() // 定义静态方法 { // 直接调用外部类的静态成员 System.out.println(b+"+"+y+"="+(b+y)); } } public void getInfo() { // 通过内部类对象访问静态内部类的非静态方法 new Inter().add(); // 直接通过"静态内部类.静态方法"形式访问静态内部类的静态方法 Inter.mul(); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); // 创建外部类实例 out.getInfo(); Outer.Inter in = new Outer.Inter(); // 创建内部类实例 in.add(); in.mul(); } } /* 4+100=104 5+200=205 4+100=104 5+200=205 */ // ============= /* 局部内部类: 是指定义在方法体内的内部类,局部内部类仅在该方法里有效,因此, 局部内部类不能被外部类和外部类以外的其它类访问,所以局部内部类 不需要访问控制符和 static 修饰符的 */ class Outer { public void fun() { class Inter { public int i = 90; } Inter in = new Inter(); System.out.println(in.i); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); out.fun(); } } // ============= /* 局部内部类: 是指定义在方法体内的内部类,局部内部类仅在该方法里有效,因此, 局部内部类不能被外部类和外部类以外的其它类访问,所以局部内部类 不需要访问控制符和 static 修饰符的 */ class Outer { public void fun() { class Inter // 定义局部内部类 { public int i = 90; } Inter in = new Inter(); // 创建局部内部类的实例 System.out.println(in.i); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); // 创建外部类实例 out.fun(); } } // ============ /* 局部内部类可以访问外部类的所有成员,包括私有成员。 若局部内部类定义在静态方法体内时,局部内部类不能直接访问外部类的非静态成员, 但是可以访问外部类的静态成员。若需要调用外部类的非静态成员时, 可以通过外部类的实例。 */ class Outer { private float f = 1.0F; //实例属性 private static int x = 90; // 静态属性 public void fun1() // 实例方法 { class Inter1 // 定义局部内部类 { private float interF = 4.0F; public Inter1() // 构造方法 { System.out.println(f+"+"+interF+"="+(f+interF)); } } new Inter1(); //匿名对象 } public static void fun2() // 静态方法 { class Inter2 { private int i = 10; public Inter2() { System.out.println(i+"+"+x+"="+(i+x)); float temp = new Outer().f; System.out.println(i+"+"+temp+"="+(i+temp)); } } new Inter2(); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); out.fun1(); out.fun2(); } } // ========== /* 匿名内部类是指在定义时没有名称的内部类,必须在声明时候时使用 new 主义声明类. 匿名内部类是一种特殊的内部类,除了具有普通内部类的特点以外,还有自己的一些特点。 匿名内部类一般只使用一次 格式: new <类/接口>([参数列表]){ ...... } 虽然匿名内部类没有类名,匿名内部类必须扩展一个基类或实现一个接口,但不要显式地 使用 extends 或 implements 关键字。如果匿名内部类继承抽象类或实现接口时,还要实现 父类及接口中所有的抽象方法。 */ // 以下的匿名内部类继承了抽象类 abstract class AbstractClass // 定义抽象类 { public abstract void getInfo(); // 声明抽象方法 } class Inter { public void print() { show(new AbstractClass(){ // 匿名内部类 public void getInfo() { System.out.println("Hi,lin3615"); } }); } public void show(AbstractClass a) // 传入抽象类实例 { a.getInfo(); // 调用抽象类方法 } } public class Hi { public static void main(String[] args) { new Inter().print(); // 创建 Inter类的实例并调用 print方法 } } // ============ //定义类 访问控制符 [修饰符] class 类名 { 访问控制符 [修饰符] 数据类型 属性名; .....; 访问控制符 [修饰符] 数据类型 方法名(参数列表) { } } 访问控制符:主要有 默认控制符(无关键词),public 修饰符: static final abstract ... // 创建对象 // 格式1 类名 对象名=null; 对象名 = new 类名(); // 格式2 类名 对象名 = new 类名(); class Person { String name; int age; public void say() { System.out.println("my name is "+name+",age is "+age); } } public class Hi { public static void main(String[] args) { Person p1 = null; p1 = new Person(); Person p2 = new Person(); Person p3 = p2; p1.name="小二"; p1.age = 26; p1.say(); // 输出 } } // ================= // ------------- class Person { private String name; private int age; public void setName(String nn) { this.name = nn; } public void setAge(int aa) { this.age = aa; } public void say() { System.out.println("my name is "+name+",age is "+age); } } public class Hi { public static void main(String[] args) { Person p1 = null; p1 = new Person(); p1.setName("lin3615"); p1.setAge(26); p1.say(); } } // ==================== 匿名对象:指没有栈空间的对象,即没有没有明显给出名字的对象。 匿名对象使用的是堆内存,是通过关键词new进行开辟的,只能使用一次 class Person { private String name; private int age; public Person(String s, int a) { this.name =s; this.age = a; } public void setName(String nn) { this.name = nn; } public void setAge(int aa) { this.age = aa; } public void say() { System.out.println("my name is "+name+",age is "+age); } } public class Hi { public static void main(String[] args) { Person p1 = new Person("lin", 26); p1.say(); } } // 构造函数,也可重载,如果没有显示声明,则调用系统默认的,(是一个的无参方法) 构造方法与类名一致 不能有任何返回值的类型声明,包括 void 不能使用return 不能被static,final,abstract,native和synchronized 修饰 // 创建构造方法: 格式1: 访问控制符 类名() { // } 格式2: 访问控制符 类名(参数列表) { // } // static 静态,java不没有传统意义上的全局,所以可用此表示全局 public class Hi { public static int ticket = 10; //定义静态变量 public void sell() // 定义sell()方法 { ticket--; System.out.println("剩下 "+ticket); } public static void left() // 定义静态方法 left() { int i = ticket-2; System.out.println("还剩下:"+i); } public static void main(String[] args) { int x = ticket--; // 直接访问 ticket System.out.println("x = "+x+", ticket = "+ticket); int y = Hi.ticket--; // 通过类名访问 System.out.println("y = "+y+" ticket = "+ticket); Hi str = new Hi(); str.left(); // 通过非静态访问 int k = str.ticket--; // 通过实例访问 System.out.println("k = "+k+", ticket = "+ticket); System.out.println(Hi.ticket); // 通过类访问 left(); // 通过静态访问 Hi.left(); // 通过非静态访问 } } /* x = 10, ticket = 9 y = 9 ticket = 8 还剩下:6 k = 8, ticket = 7 7 还剩下:5 还剩下:5 */ // ======================== public class Hi { { // begin 构造块 System.out.println("构造块1"); } // end 构造块 { // begin 构造块 System.out.println("构造块2"); } // end 构造块 public Hi() { System.out.println("构造方法"); } public static void main(String[] args) { new Hi(); new Hi(); new Hi(); } } /* 构造块1 构造块2 构造方法 构造块1 构造块2 构造方法 构造块1 构造块2 构造方法 */ //==================== // 静态代码块只会执行一次 class Code { { System.out.println("code的构造块"); // 构造代码块 4 } static { System.out.println("code 的静态代码块"); // 静态代码块 3 } public Code() { System.out.println("code 的构造方法普通代码块"); // 构造方法 } } public class Hi { { System.out.println("Hi 的构造块"); // 构造代码块 } static { System.out.println("Hi 的静态代码块"); // 静态代码块 1 } public Hi() { System.out.println("hi 的构造方法普通块"); // 构造方法 } public static void main(String[] args) { System.out.println("Hi 的主体方法"); //2 new Code(); new Code(); new Hi(); new Hi(); } } /** 静态代码块只会执行一次,无论在一次中,实例多少都是一次 Hi 的静态代码块 Hi 的主体方法 code 的静态代码块 code的构造块 code 的构造方法普通代码块 code的构造块 code 的构造方法普通代码块 Hi 的构造块 hi 的构造方法普通块 Hi 的构造块 hi 的构造方法普通块 */ // ============ // 使用 package 定义包 // 定义包就是将声明的类放入包中,为包指定包名,格式如下: // package 包名1[.子包名1.子包名2....]; package org.com.pack; public class Hi { public static void main(String[] args) { System.out.println("定义包"); } } // package 语句必须是程序中可执行的第一行代码 // package 语句只能有一句 // 在前面的包名是 后面的包名 的父目录 // 没有 package 语句,则默认无包名 // 用javac 编译,编译如下: // javac -d . Hi.java // -d 表示生成目录. . 表示目录的位置,此表示生成为当前目录,最终会生成 org/com/pack/Hi.class // 执行时用 java 包路径.类名: java org.com.pack.Hi // 当定义包后,同一个包中的类是默认导入的,如果一个类访问来自另一个包中的类时, // 前者必须通过 import 语句把 后者导入才能使用。 // import 语法格式 // 格式1:import 包名[.子包名...].类名; 只会导入当前的类 // 格式2: import 包名[.子包名 ...].*; 会导入包中所有的类,但不会导入其子包中的类,要用到时还得手动导入 // =================== // 建立 Student.java, Student.class 位于 org.com.pack.stu中 package org.com.pack.stu; public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { setName(name); setAge(age); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } public void show() { System.out.println("my name is "+this.name+" and age is "+this.age); } } // 建立 Hi.java, 生成的类位于包 org.com.pack.pack中 package org.com.pack.pack; import org.com.pack.stu.Student; public class Hi { public static void main(String[] args) { Student stu = new Student("lin3615", 26); stu.show(); } } // javac -d . 文件1.java 文件2.java ...可以连接多个一起编译 // javac -d . Student.java Hi.java // java org.com.pack.pack.Hi // ===================== // 静态导入 // Student.java package org.com.pack.area; public class Student { public static double PI = 3.14; public static double round(int r) { return PI*r*r; } public static int square(int a) { return a*a; } } // Hi.java package org.com.pack.pack; import static org.com.pack.area.Student.*; public class Hi { public static void main(String[] args) { System.out.println("PI = "+PI); System.out.println("ROUND = "+round(10)); System.out.println("square = "+square(10)); } } // javac -d . Student.java Hi.java // java org.com.pack.pack.Hi // 手动装箱/拆箱 public class Hi { public static void main(String[] args) { int x = 100; Integer in = new Integer(x); // 手动装箱 System.out.println(in); Float f = new Float(3.14F); float y = f.floatValue(); // 手动拆箱 System.out.println(y); } } // =================== // ----------- public class Hi { public static void main(String[] args) { int x = 100; Integer in = x; // 自动装箱 System.out.println(in); Float f = 10.98F; float y = f; // 自动拆箱 System.out.println(f); } } // 转换成字符串,用封装中的 toString() 方法 public class Hi { public static void main(String[] args) { int x = 100; Integer in = new Integer(x); String ii = in.toString(); Float f = new Float(10.90F); String ff = f.toString(); Double d = new Double(89.76D); String dd = d.toString(); System.out.println(ii+","+ff+","+dd); } } // ================ // 字符转为数值 parseXxx(string str)/ parseXxx(String str, int i),其中 Xxx: Int,Double ... public class Hi { public static void main(String[] args) { String ii = "333"; String dd = "1.99"; int i = Integer.parseInt(ii); // 字符串转为整型 double d = Double.parseDouble(dd); // 字符串转为浮点 System.out.println(i*d); } } // =================== // -------------- // final 变量,属性,引用,其中引用的属性可以改变 public class Hi { final float PI = 3.14f; final int arrInt[] = {1,2,3,4}; final int num; final String str; { num = 100; } public Hi() { str = "lin3615"; } public static void main(String[] args) { final char c = 'A'; Hi p = new Hi(); System.out.println("c = "+c); System.out.println("PI = "+p.PI); System.out.println("num = "+p.num); System.out.println("Str = "+p.str); for(int i=0; i<4; i++) { System.out.print(p.arrInt[i]+" "); } System.out.println(); for(int j = 0; j< 4; j++) { p.arrInt[j] = p.arrInt[j] * 10; } for(int j = 0; j< 4; j++) { System.out.print(p.arrInt[j]+" "); } } } // ================ // ------------ // final 方法不可继承,但可重写 class Base { public final void add(int x, int y) { System.out.println("x+y = "+(x+y)); } } class Sub extends Base { public final void add(int x, int y, int z) { System.out.println("x+y+z = "+(x+y+z)); } } public class Hi { public static void main(String[] args) { Sub sub = new Sub(); sub.add(1,2); sub.add(1,2,3); } } // ============== // 如果子类没有继承父类的抽象方法,则报错 abstract class Person { public static final String contry = "China"; private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public abstract void tell(); // public abstract void say(); } class Student extends Person { public void tell() { System.out.println("my name "+this.getName()+", and "+this.contry); } } public class Hi { public static void main(String[] args) { Student stu = new Student(); stu.setName("lin3615"); stu.tell(); } } // ============= // 调用抽象类中的构造方法,隐含 super(); abstract class Person { public Person() { System.out.println("这是父类的构造方法"); } } class Student extends Person { public Student() { System.out.println("这是 student 方法"); } } public class Hi { public static void main(String[] args) { Student stu = new Student(); } } // 这是父类的构造方法 // 这是 student 方法 // ============== // 显示用 supper() abstract class Shape { private float width; private float high; public Shape() { } public Shape(float width, float high) { this.width = width; this.high = high; } public void setWidth(float width) { this.width = width; } public float getWidth() { return this.width; } public void setHigh(float high) { this.high = high; } public float getHigh() { return this.high; } public abstract void area(); public abstract void cir(); } class Rectangle extends Shape { public Rectangle() { } public Rectangle(float width, float high) { super(width, high); } public void area() { System.out.println(this.getWidth() * this.getHigh()); } public void cir() { System.out.println(this.getWidth() * this.getWidth()); } } public class Hi { public static void main(String[] args) { Rectangle rec = new Rectangle(1.8f,2.1f); rec.area(); rec.cir(); } } // -============ // 接口 /** 格式 [public] interface 接口名称 [extends 父接口1,父接口2,..] { [public static final ] 数据类型 变量名 = 初值; // 默认就是 public static final [public abstract] [native ] 返回类型 方法体([参数列表]); // 默认就是 public abstract } */ interface InterShape { public static final float PI = 3.14F; public abstract void getArea(); public abstract void getCir(); } class Round implements InterShape { private float radius; public Round() { } public Round(float radius) { this.radius = radius; } public void setRound(float radius) { this.radius = radius; } public float getRound() { return this.radius; } public void getArea() { System.out.println(this.getRound() * InterShape.PI); } public void getCir() { } } public class Hi { public static void main(String[] args) { Round rr = new Round(3.9F); rr.getArea(); } } // =============== // 抽象类实现接口 interface InterShape { public static final float PI = 3.14f; public abstract void getArea(); public abstract void getCir(); } abstract class Sphere implements InterShape { public abstract void getVolume(); } class Round extends Sphere { private float radius; public Round() { } public float getRadius() { return this.radius; } public Round(float radius) { this.radius = radius; } public void getArea() { System.out.println(InterShape.PI*this.getRadius()); } public void getCir() { System.out.println(InterShape.PI+this.getRadius()); } public void getVolume() { System.out.println(InterShape.PI-this.getRadius()); } } public class Hi { public static void main(String[] args) { Round rr = new Round(10.88f); rr.getArea(); } } // ================ // 接口多重继承 // [public] interface 子接口 extends 父接口A[,父接口B....]{} interface Ok { public static final float PI = 3.14f; } interface InterShape { public abstract void getArea(); } interface InterTwo extends Ok,InterShape { public abstract void getCir(); } abstract class Sphere implements InterTwo { public abstract void getVolume(); } class Round extends Sphere { private float radius; public Round() { } public float getRadius() { return this.radius; } public Round(float radius) { this.radius = radius; } public void getArea() { System.out.println(InterTwo.PI*this.getRadius()); } public void getCir() { System.out.println(InterTwo.PI+this.getRadius()); } public void getVolume() { System.out.println(InterTwo.PI-this.getRadius()); } } public class Hi { public static void main(String[] args) { Round rr = new Round(10.88f); rr.getArea(); } } // ============ // 对象数组 // 类名 对象数组名[] = new 类名[数组长度] // 动态初始化 class School { private String name; public School(){} public School(String name) { setName(name); } public void setName(String name) { this.name = name; } public String getName() { return this.name; } } public class Hi { public static void main(String[] args) { School school[] = new School[4]; school[0] = new School("一加大学"); school[1] = new School("二个大学"); school[2] = new School("三个大学"); school[3] = new School("aaaaa"); for(School a:school) { System.out.print(a.getName()+" "); } } } // ================ // 静态初始化 class School { private String name; public School(){} public School(String name) { setName(name); } public void setName(String name) { this.name = name; } public String getName() { return this.name; } } public class Hi { public static void main(String[] args) { School school[] = {new School("aa"), new School("bbb"), new School("cccc")}; for(School aa:school) { System.out.print(aa.getName()+" "); } } } // =================== // 内部类 /* [public ] class 外部类名 { [访问控制符] [修饰符] 成员; ... [访问控制符] [修饰符] class 内部类 { [访问控制符] [修饰符] 成员; } } */ // 定义内部类 class Outer { private String str = "lin3615"; public class Inter { public void add(int x, int y) { System.out.println("x+y = "+(x+y)); } public void getStr() { System.out.println(str); } } public void getInfo() { new Inter().getStr(); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); out.getInfo(); Outer.Inter oin = new Outer().new Inter(); oin.add(3,5); oin.getStr(); } } // ================= // ========== /* 直接使用 new Inter() 为接口实例化,但接口本身是不能实例化的, 所以在 new Inter() 之后的花括号 {} 中写的就是实现接口中的抽象方法 */ interface Inter // 定义接口 { public abstract void getInfo(); // 声明抽象方法 } class InterClass // 定义类 { public void print() { show(new Inter(){ // 匿名内部类 public void getInfo() { System.out.println("这是匿名内部类实现接口"); } }); } public void show(Inter i) // 传入接口 { i.getInfo(); // 调用抽象类方法 } } public class Hi { public static void main(String[] args) { new InterClass().print(); // 创建 InterClass 类的实例并调用print()方法 } } // =============