代码演示:
package com.pxcoder.myproject4;
//外部类
public class Test02 {
int num=10;
public static void main(String[] args) {
}
public void fun() {
System.out.println("我是普通外部类方法");
}
//普通内部类
class inner02{
String innername="小明";
public inner02() {
}
public void method() {
System.out.println("我是普通内部类方法");
}
}
}
定义:
代码演示:
package com.pxcoder.myproject4;
public class Test02 {
int num=10;
public static void main(String[] args) {
Test02 b=new Test02();
b.fun();
}
public void fun() {
System.out.println("我是普通外部类方法");
//创建内部类对象来调用内部类中的方法和属性
inner02 a=new inner02();
a.method();
System.out.println(a.innername);
}
class inner02{
String innername="小明";
public inner02() {
}
public void method() {
System.out.println("我是普通内部类方法");
}
}
}
代码演示:
package com.pxcoder.myproject4;
public class Test02 {
int num=10;
public static void main(String[] args) {
Test02 b=new Test02();
b.fun();
}
public void fun() {
System.out.println("我是普通外部类方法");
inner02 a=new inner02();
a.method();
System.out.println(a.innername);
}
class inner02{
String innername="小明";
public inner02() {
}
public void method() {
System.out.println("我是普通内部类方法");
System.out.println(num);
System.out.println(Test02.this.num);
}
}
}
//执行结果:
我是普通外部类方法
我是普通内部类方法
10
10
小明
代码演示:
//方法一
package com.pxcoder.myproject4;
//导包
import com.pxcoder.myproject4.Test02.inner02;
public class Test002 {
public static void main(String[] args) {
inner02 in=new Test02().new inner02();
in.method();
}
}
//方法二
package com.pxcoder.myproject4;
//导包
import com.pxcoder.myproject4.Test02.inner02;
public class Test002 {
public static void main(String[] args) {
Test02.inner02 in=new Test02().new inner02();
in.method();
}
}
package com.pxcoder.myproject4;
//外部类
public class Test02 {
int num=10;
public void fun() {
System.out.println("我是普通外部类方法");
}
//内部类
class inner02{
String innername="小明";
public inner02() {
}
public void method() {
System.out.println("我是普通内部类方法");
System.out.println(num);
}
}
}
tips:对于内部类而言,编译之后的源文件的class文件为:外部类$内部类.class
代码演示:
package com.pxcoder.myproject4;
//外部类
public class Test02 {
int num=10;
public static void main(String[] args) {
}
public void fun() {
System.out.println("我是普通外部类方法");
}
//静态内部类
static class inner02{
String innername="小明";
public inner02() {
}
public void method() {
System.out.println("我是静态内部类方法");
}
public static void method1() {
System.out.println("我是静态内部类的静态方法");
}
}
}
定义;
代码演示:
package com.pxcoder.myproject4;
public class Test02 {
int num=10;
public static void main(String[] args) {
Test02 b=new Test02();
b.fun();
}
public void fun() {
System.out.println("我是普通外部类方法");
//创建对象访问静态内部类中的非静态的成员
inner02 a=new inner02();
a.method();
System.out.println(a.innername);
//直接访问静态内部类的静态成员
inner02.method1();
System.out.println(inner02.age);
}
static class inner02{
String innername="小明";
static int age=23;
public inner02() {
}
public void method() {
System.out.println("我是静态内部类方法");
}
public static void method1() {
System.out.println("我是静态内部类的静态方法");
}
}
}
类名.属性/方法名
访问。代码演示:
package com.pxcoder.myproject4;
import org.omg.CORBA.PUBLIC_MEMBER;
public class Test02 {
int num=10;
public static void main(String[] args) {
Test02 b=new Test02();
b.fun();
}
public void fun() {
System.out.println("我是普通外部类方法");
//创建对象访问静态内部类中的非静态的成员
inner02 a=new inner02();
a.method();
//直接访问静态内部类的静态成员
inner02.method1();
}
public void mage() {
System.out.println("我是外部类的方法");
}
static class inner02{
String innername="小明";
static int age=23;
public inner02() {
}
public void method() {
System.out.println("我是静态内部类方法");
//创建外部类对象来访问外部类的成员
Test02 b=new Test02();
b.mage();
b.num;
}
public static void method1() {
System.out.println("我是静态内部类的静态方法");
Test02 b=new Test02();
b.mage();
}
}
}
代码演示:
package com.pxcoder.myproject4;
//导包
import com.pxcoder.myproject4.Test02.inner02;
public class Test002 {
public static void main(String[] args) {
//访问静态内部类的静态成员
System.out.println(inner02.age);
inner02.method1();
Test02.inner02.method1();
//访问静态内部类的非静态成员
inner02 on=new inner02();
on.method();
inner02 in=new Test02.inner02();
in.method();
}
}
package com.pxcoder.myproject4;
//外部类
public class Test02 {
int num=10;
public void fun() {
System.out.println("我是普通外部类方法");
}
public void mage() {
System.out.println("我是外部类的方法");
}
//静态内部类
static class inner02{
String innername="小明";
static int age=23;
public inner02() {
}
public void method() {
System.out.println("我是静态内部类方法");
}
public static void method1() {
System.out.println("我是静态内部类的静态方法");
}
}
}
代码演示:
package com.pxcoder.myproject4;
//外部类
public class Test03 {
public static void main(String[] args) {
method();
}
public static void method() {
//局部内部类
class inner03{
int age;
public void mage() {
System.out.println("我是一个局部内部类方法");
}
}
//创建局部内部类的对象调用局部内部类的成员
inner03 a=new inner03();
a.mage();
System.out.println(a.age);
}
}
package com.pxcoder.myproject3;
//统一接口
public interface Filter1 {
public abstract void filter(Student[] stus);
}
package com.pxcoder.myproject3;
public class Student {
private int id;
private String name;
private int age;
private int score;
public Student() {
}
public Student(int id,String name,int age,int score) {
this.id=id;
this.name=name;
this.age=age;
this.score=score;
}
public void setScore(int score) {
this.score=score;
}
public int getScore() {
return score;
}
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return age;
}
public String toString() {
return name+" "+id+" "+age+" "+score;
}
}
package com.pxcoder.myproject3;
public class Demo02{
public static void main(String[] args) {
//声明一个学生数组,存放5个学生信息
Student stu1=new Student(1,"张山",12,60);
Student stu2=new Student(2,"张三",15,66);
Student stu3=new Student(3,"李四",34,70);
Student stu4=new Student(4,"小二",49,80);
Student stu5=new Student(5,"王二",18,90);
//声明一个学生数组 存储学生对象
Student[] stus=new Student[] {stu1,stu2,stu3,stu4,stu5};
//匿名内部类使用,接口不可以实例化
filter(new Filter1(){
@Override
public void filter(Student[] stus){
for(Student stu:stus){
if(stu.getAge()>20&&stu.getAge()<50){
System.out.println(stu+",");}
}
}
},stus);
}
public static void filter(Filter1 filter,Student[] stus) {
//Filter filter = new AgeFilter()
filter.filter(stus);//多态
}
}
//执行结果:
李四 3 34 70,
小二 4 49 80,
关于Lambda表达式:
public class Test03{
public static void main(String[] args){
//方法一
MyRun r=new MyRun();
new Thread(r);
//方法二
new Thread(new Runnable(){
public voidrun(){
System.out.println("匿名內部類");
}
});
//方法三
new Thread(()->{System.out.println("匿名內部類");});
}
}
class MyRun implements Runnable{
@Override
public void run(){
System.out.println("myrun");
}
}