public static void 方法名() {
//f方法体;
}
方法名();
public class Demo {
public static void main(String[] args) {
demo(); //main方法中调用其他方法
}
public static void demo() {
int a = 9;
if(a%2==0) {
System.out.println("偶数");
}else {
System.out.println("奇数"); //奇数
}
}
}
public class Demo {
public static void main(String[] args) {
getMax();
}
public static void getMax() {
int a = 10;
int b = 20;
if(a>b) {
System.out.println("a更大");
}else {
System.out.println("b更大"); //b更大
}
}
}
public static void 方法名(参数) {...}
public static void 方法名(数据类型 变量名) {...}
public static void getMax(int arr) {...}
public static void 方法名(数据类型 变量名1,数据类型 变量名2,...) {...}
public static void getMax(int arr1,int arr2,...) {...}
方法名(参数);
方法名(变量名/常量名);
getMax(5);
方法名(变量名1/常量名1,变量名2/常量名2,...);
getMax(5,6);
注意
:方法调用时,参数的数量和类型必须和方法定义中的设置相匹配,否则就会报错
案例1:判断奇偶数
public class Demo {
public static void main(String[] args) {
//常数值得调用
IsEvenNumber(10); //偶数
//变量的调用
int num = 10;
IsEvenNumber(num); //偶数
}
public static void isEvenNumber(int number) {
if(number%2 == 0) {
System.out.println("偶数");
}else {
System.out.println("奇数");
}
}
}
形参和实参
形参:方法定义中的参数,例如:int number
实参:方法调用中的参数,例如:10、num
案例1:比较大小
public class Demo {
public static void main(String[] args) {
getMax(10,90); //b更大
}
public static void getMax(int a,int b) {
if(a>b) {
System.out.println("a更大");
}else {
System.out.println("b更大");
}
}
}
public static 数据类型 方法名(参数) {
return 数据:
}
//案例1
public static boolean isEvenNumber(int number){
return true;
}
//案例2
public static int getMax(int a,int b){
return 100;
}
注意
:方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则报错
调用格式
//格式一: 方法名(参数);
isEvenNumber(5);
//格式二: 数据类型 变量名 = 方法名(参数);
boolean flag = isEvenNumber(5);
注意
:方法的返回值通常使用变量接受(格式二),否则该返回值无意义public class Demo {
public static void main(String[] args) {
//1.方法名(参数);
//idEvenNumber(10); //结果为true,但无返回值
//2.数据类型 变量名= 方法名(参数);
boolean flag = idEvenNumber(10);
//boolean flag = true;
System.out.println(flag); //true
}
public static boolean idEvenNumber(int number) {
if(number%2 == 0) {
return true;
}else {
return false;
}
}
}
public class Demo {
public static void main(String[] args) {
int result = getMax(10,20);
System.out.println(result+"更大");
}
public static int getMax(int a,int b) {
if(a>b) {
return a;
}else {
return b;
}
}
}
public static 返回值类型 方法名(参数){
方法体;
return 数据;
}
public class Demo {
public static void main(String[] args) {
System.out.println(compare(10,20)); //int false
System.out.println(compare((short)10,(short)20)); //short false
}
public static boolean compare(int a,int b) {
System.out.println("int");
return a==b;
}
public static boolean compare(byte a,byte b) {
System.out.println("byte");
return a==b;
}
public static boolean compare(short a,short b) {
System.out.println("short");
return a==b;
}
public static boolean compare(long a,long b) {
System.out.println("long");
return a==b;
}
}
public class Demo {
public static void main(String[] args) {
int[] arr = {11,22,33,44,55};
printArry(arr); //[11,22,33,44,55]
}
public static void printArry(int[] arr) {
System.out.print("[");
for(int i=0;i< arr.length;i++) {
if(i == arr.length-1) { //arr.length-1取到最后一位
System.out.print(arr[i]);
}else {
System.out.print(arr[i]+", ");
}
}
System.out.print("]");
}
}
public class Demo {
public static void main(String[] args) {
//1、定义数组,静态初始化
int[] arr = {11,22,33,44,55};
//3、调用方法,用变量接受返回值
int number = getMax(arr);
System.out.println(number); //55
}
//2、定义方法获取最大值
//两个明确:返回值类型:int(因为最大值)参数:int[] arr
public static int getMax(int[] arr) {
int max = arr[0];
for(int i=1;i<arr.length;i++) {
if(arr[i]>max) {
max = arr[i];
}
}
return max;
}
}
new Phone();
,按下Ctrl+alt+v自动补全前面Phone p = new Phone();
public class 类名{
// 成员变量
变量1的数据类型 变量1;
变量2的数据类型 变量2;
...
//成员方法
方法1;//没有static
方法2;
...
//范例
public class Phone {
//成员变量
String brand;
int price;
//成员方法
public void call(){
System.out.println("打电话");
}
public void senMessage(){
System.out.println("发短信");
}
}
// 类名 对象名 = new 类名();
Phone p = new Phone();
new Phone();
,按下Ctrl+alt+v自动补全前面Phone p = new Phone();
p.brand;
p.call();
public class PhoneDemo {
public static void main(String[] args) {
//创建对象:类名 变量名 = new 类名();
Phone p = new Phone();
//使用成员变量
System.out.println(p.brand); //null
System.out.println(p.price); //0
p.brand = "小米";
p.price = 2999;
System.out.println(p.brand); //小米
System.out.println(p.price); //2999
//成员方法
p.call(); //电话
p.senMessage(); //发短信
}
}
public class Student {
//成员变量
String name;
int age;
//成员方法
public void study(){
System.out.println("学习");
}
public void doHomeWork() {
System.out.println("做作业");
}
}
public class StudentDemo {
public static void main(String[] args) {
Student stu = new Student();
//调用成员变量
stu.name = "袁满";
stu.age = 20;
System.out.println(stu.name); //袁满
System.out.println(stu.age); //20
//调用成员方法
stu.study(); //学习
stu.doHomeWork(); //做作业
}
}
public class Student {
//成员变量
String name;
private int age;
//提供get、set方法
public void setAge(int a) {
if(a<0 || a>120) {
System.out.println("你给的值有误");
}else {
age = a;
}
}
public int getAge(){
return age;
}
public void show() {
System.out.println(name+","+age);
}
}
public class StudentDemo {
public static void main(String[] args) {
//创建对象
Student stu = new Student();
//调用成员变量
stu.name = "袁满";
//stu.age = 20; //不能使用因为private
//stu.setAge(30); //袁满,30
stu.setAge(-30); //你给的值有误 袁满,0
//调用成员方法
stu.show();
}
}
public class Student {
//成员变量
private String name;
private int age;
//提供get/set方法
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
public void show () {
System.out.println(name+"," + age);
}
}
public class StudentDemo {
public static void main(String[] args) {
//创建对象
Student stu = new Student();
//调用成员变量
stu.setName("袁满");
stu.setAge(20);
stu.show();//袁满,20
//使用get方法获取成员变量的值
System.out.println(stu.getName()+"---"+stu.getAge()); //袁满---20
}
}
public class 类名{
修饰符 类名(参数) { //修饰符一般使用public
}
}
//案例
public class Student {
public Student() {
//构造方法内容
}
}
public class Student {
//成员变量
private String name;
private int age;
//构造方法
public Student() {
System.out.println("无参构造方法");
}
public void show() {
System.out.println(name+"," +age);
}
}
public class StudentDemo {
public static void main(String[] args) {
//创建对象
Student stu = new Student(); //调用的是Student类的构造方法
// 执行时机
// 1.创建对象的时候调用,每创建一次对象,就会执行一次构造方法
// 2.不能手动调用构造方法
stu.show(); //无参构造方法
//null,0
}
}
1、如果没有定义构造方法,系统将给出一个默认的无参数构造方法
2、如果定义了构造方法,系统将不再提供默认的构造方法
解决方法:无论是否使用,都手动书写无参数构造方法,和带参数构造方法
public class Student {
//private封装成员变量
private String name;
private int age;
//无参构造方法
public Student() {
}
//有参构造方法
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//成员方法set/get
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void show() {
System.out.println(name + "," + age);
}
}
public class StudentDemo {
public static void main(String[] args) {
//1.无参构造方法创建对象,使用set方法赋值
Student stu = new Student();
stu.setName("袁满");
stu.setAge(20);
stu.show(); //袁满,20
//2.带多个参数构造方法创建对象
Student stu1 = new Student("袁满",12);
stu1.show(); //袁满,12
}
}
public class 子类名 extends 父类名{}
public class Zi extends Fu{}
//Fu:是父类,也称基类、超类
//Zi:是子类,也称派生类
package ceshi;
public class Fu {
public int age=20;
}
package ceshi;
public class Zi extends Fu{
public int height=175;
public int age = 30;
public void show(){
int age =40;
System.out.println(age); //40
System.out.println(height);
//System.out.println(weight); //报错
}
}
package ceshi;
public class Deme {
public static void main(String[] args) {
Zi z = new Zi();
z.show(); //40 175
}
}
package ceshi;
public class Fu {
public int age=20;
}
package ceshi;
public class Zi extends Fu{
public int age = 30;
public void show(){
int age =40;
System.out.println(age); //40
//1.访问本类中的成员变量age
System.out.println(this.age); //30
//2.访问父类中的成员方法
System.out.println(super.age); //20
}
}
package ceshi;
public class Deme {
public static void main(String[] args) {
Zi z = new Zi();
z.show(); //40 30 20
}
}
package ceshi;
public class Fu {
/*public Fu() {
System.out.println("Fu中无参构造方法");
}*/
public Fu(){};
public Fu(int age) {
System.out.println("Fu中带参构造方法");
}
}
package ceshi;
public class Zi extends Fu{
public Zi() {
// super(); //默认有所以不用写
System.out.println("Zi中无参构造方法");
}
public Zi(int age) {
// super();
System.out.println("Zi中带参构造方法");
}
}
package ceshi;
public class Deme {
public static void main(String[] args) {
Zi z = new Zi();
Zi z1 = new Zi(20);
/*Fu中无参构造方法
Zi中无参构造方法
Fu中无参构造方法
Zi中带参构造方法*/
}
}
package ceshi;
public class Fu {
public void show(){
System.out.println("Fu中show方法调用");
}
}
package ceshi;
public class Zi extends Fu{
public void method() {
super.show();
System.out.println("Zi中method方法被调用");
}
}
package ceshi;
public class Deme {
public static void main(String[] args) {
Zi z = new Zi();
z.method();
z.show();
// z.text() //报错
/*Fu中show方法调用
Zi中method方法被调用
Fu中show方法调用*/
}
}
package ceshi;
public class Phone {
public void call(String name) {
System.out.println("给"+name+"打电话");
}
}
public class NewPhone extends Phone{
@Override
public void call(String name) {
System.out.println("开启视频");
// System.out.println("给"+name+"打电话");
super.call(name);
}
}
package ceshi;
public class PhoneDemo {
public static void main(String[] args) {
Phone p = new Phone();
p.call("袁满"); //给袁满打电话
NewPhone n = new NewPhone();
n.call("袁满1");
/*开启视频
给袁满1打电话*/
}
}
package ceshi;
public class Fu {
private void show() {
System.out.println("Fu中show方法被调用");
}
public void method() {
System.out.println("Fu中method方法被调用");
}
}
package ceshi;
public class Zi extends Fu{
/*@Override //报错,私有方法不能被继承
private void show() {
System.out.println("Zi中show方法被调用");
}*/
void method() { //报错,访问权限低于public,把父类public去掉就可以
System.out.println("Fu中method方法被调用");
}
}
package ceshi;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package ceshi;
public class Teacher extends Person{
public Teacher() {};
public Teacher(String name,int age) {
/*this.name = name;
this.age = age;*/
/*super.name = name;
super.age = age;*/ //私有成员没有用
super(name,age); //通过父类的带参构造方法给成员变量赋值
}
public void teach() {
System.out.println("教学生");
}
}
package ceshi;
public class Deme {
public static void main(String[] args) {
//创建无参构造方法,老师类测试
Teacher t1 = new Teacher();
t1.setName("yy");
t1.setAge(20);
System.out.println(t1.getName()+"-"+t1.getAge()); //yy-20
t1.teach(); //教学生
//带参
Teacher t2 = new Teacher("yyy",30);
System.out.println(t2.getName()+"-"+t2.getAge()); //yyy-30
t2.teach(); //教学生
}
}
package ceshi;
/*public class Fu {
public final void method() {
System.out.println("Fu中method方法被调用");
}
}*/
public final class Fu {
public final void method() {
System.out.println("Fu中method方法被调用");
}
}
package ceshi;
public class Zi extends Fu{ //报错了,不能被继承
/*@Override //因为父类method方法啊被final修饰无法重写
public void method(){
System.out.println("zi中method");
}*/
public final int age = 20;
public void show() {
// age = 100; 被final修饰为常量无法改变
System.out.println(age);
}
}
package ceshi;
public class Student {
public int age = 20;
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//final修饰基本类型变量
final int age =20;
// age = 100; //final修饰后就不能重新赋值
System.out.println(age); //20
//final修饰引用类型变量
final Student s = new Student(); //final修饰的s,s的地址不能变,s.age是内容
s.age =100;
System.out.println(s.age); //100
//反证
// s = new Student(); //报错
}
}
static关键字是静态的意思,可以修饰成员变量,成员方法
static修饰的特点:
按照有无static修饰,成员变量和方法可以分为:
(一)成员变量
1、静态成员变量(类变量):有static修饰的成员变量称为静态成员变量,也叫类变量,属于类本身,直接用类名访问即可
2,、实例成员变量:无static修饰的成员变量称为实例成员变量,属于类的每个对象的,必须用类的对象来访问
(二)成员方法
1、静态方法:有static修饰的成员方法称为静态方法,也叫类方法,属于类本身的,直接用类名访问即可
2、实例方法:无static修饰的成员方法称为实例方法,属于类的每个对象的,必须用类的对象来访问
Student类
package ceshi;
public class Student {
public String name;
public int age;
//1: public String university;
//2
public static String university;
public void show() {
System.out.println(name+","+age+","+university);
}
}
package ceshi;
public class StaticDemo {
public static void main(String[] args) {
//2:被static修饰的变量,推荐用类名访问
Student.university="清华大学";
Student s = new Student();
s.name = "yy";
s.age = 20;
//1 s.university = "清华大学";
s.show(); //yy,20,清华大学
Student s1 = new Student();
s1.name = "yy1";
s1.age = 30;
s1.show(); //1: yy1,30,null
//2: yy1,30,清华大学
}
}
非静态的成员方法
●能访问静态的成员变量
●能访问非静态的成员变量
●能访问静态的成员方法
●能访问非静态的成员方法
静态的成员方法
●能访问静态的成员变量
●能访问静态的成员方法
总结成一句话就是:静态成员方法只能访问静态成员
package ceshi;
public class Student {
//非静态成员变量
public String name = "yy";
//静态成员变量
public static String university = "清华大学";
//非静态成员方法
public void show1() {
}
public void show2() {
System.out.println(name);
System.out.println(university);
show1();
show3();
}
//静态成员方法
public static void show3() {
}
public static void show4() {
// System.out.println(name); 访问非静态成员变量报错
System.out.println(university);
// show1(); 访问非静态成员方法报错
show3();
}
}
package ceshi;
public class Animal {
public void eat(){
System.out.println("吃东西");
}
}
package ceshi;
public class Cat extends Animal{ //1,有继承/实现关系
//2,有方法重写
@Override
public void eat() { //重写快捷方法直接写eat按回车自动生成
System.out.println("猫吃东西");
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//3、有父类引用指向子类对象
Animal a = new Cat();
}
}
package ceshi;
public class Animal {
public int age = 20;
public void eat(){
System.out.println("动物吃东西");
}
}
package ceshi;
public class Cat extends Animal{ //1,有继承/实现关系
//2,有方法重写
public int age = 40;
public int weight = 10;
@Override
public void eat() { //3,重写快捷方法直接写eat按回车自动生成
System.out.println("猫吃东西");
}
public void playGame(){
System.out.println("猫玩捉迷藏");
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//有父类引用指向子类对象
Animal a = new Cat();
a.eat(); //猫吃东西
//对于方法的调用: 编译(写代码)看左边Animal,运行看右边 Cat
//看左边指的是看等号的左边,等号左边是Animal,会找Animal里面的eat方法,有方法才不报错
//运行的时候看等号的右边,等号右边是Cat,执行Cat里面的eat方法
// a.playGame; 报错,父类有没这个方法
System.out.println(a.age); //20;对于变量的调用: 编译看左边,运行看左边
// System.out.println(a.weight); 报错
}
}
package ceshi;
public class Animal {
public void eat(){
System.out.println("动物吃东西");
}
}
package ceshi;
public class Cat extends Animal{ //1,有继承/实现关系
//2,有方法重写
@Override
public void eat() { //重写快捷方法直接写eat按回车自动生成
System.out.println("猫吃东西");
}
public void playGame(){
System.out.println("猫玩捉迷藏");
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//多态
Animal a = new Cat(); //向上转型
a.eat();
// a.playGame(); 报错,因为编译看左边Animal中没有这个方法
//向下转型
Cat c = (Cat)a;
c.eat(); //猫吃东西
c.playGame(); //猫玩捉迷藏
}
}
public abstract class 类名(){
public abstract void 方法名();
}
package ceshi;
public abstract class Animal {
private int age = 20;
private final String name = "yy";
public Animal(){};
public Animal(int age) {
this.age = age;
}
public void show() {
System.out.println(age);
System.out.println(name);
}
public abstract void eat();
}
package ceshi;
public abstract class Animal {
private String name;
private int age;
public Animal() { }
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
}
package ceshi;
public class Cat extends Animal{ //1,有继承/实现关系
public Cat() { }
public Cat(String name, int age) {
super(name, age);
}
//2,有方法重写
@Override
public void eat() { //3,重写快捷方法直接写eat按回车自动生成
System.out.println("猫吃东西");
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//多态
Animal a = new Cat(); //调用无参构造方法
a.setName("加菲猫");
a.setAge(20);
System.out.println(a.getName()+","+a.getAge()); //加菲猫,20
a.eat(); //猫吃东西
//带参
a = new Cat("加菲猫1",20);
System.out.println(a.getName()+","+a.getAge()); //加菲猫1,20
a.eat(); //猫吃东西
}
}
public interface 接口名 {}
public class 类名 implement 接口名 {}
package ceshi;
public interface Inter {
//1,默认带有public static final
public int num1 = 10;
public static final int num2 = 20;
int num3 = 30;
//2,报错,接口中没有构造方法
// public Inter(); 报错
//3,接口中必须是抽象方法
public abstract void mehtod();
// public void mehtod(){}; 报错
void show(); //默认带有public abstract
}
package ceshi;
public interface Inter {
void jump();
}
package ceshi;
public abstract class Animal {
private String name;
private int age;
public Animal() { }
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat();
}
package ceshi;
public class Cat extends Animal implements Jumpping{ //2,接口不能继承,类用implement实现接口,该类为实现类
public Cat() { }
public Cat(String name, int age) {
super(name, age);
}
@Override
public void jump() {
System.out.println("猫跳高");
}
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
Jumpping j = new Cat();
j.jump(); //只有调接口的
Animal a = new Cat("加菲猫",5);
System.out.println(a.getName()+","+a.getAge());
a.eat();
// a.jump(); 报错,无法调用接口的
//推荐使用子类对象,子类中功能最多
Cat c = new Cat();
c.eat();
c.jump();
c.setName("加菲猫");
c.setAge(20);
}
}
public class IterImpl extends Object implements Inter1,Inter2,Inter3 {}
//单继承
public interface Inter3 extends Inter1 {}
//多继承
public interface Inter3 extends Inter1,Inter2 {}
package ceshi;
public class Cat{
public void eat() {
System.out.println("猫吃鱼");
}
}
package ceshi;
public class CatOperator {
public void useCat(Cat c) { //类名作为形参需要的是一个Cat对象,Cat c = new Cat();
c.eat();
}
//Cat作为返回值
public Cat getCat() {
Cat c = new Cat();
return c;
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//创建操作类对象,调用方法
CatOperator co = new CatOperator();
Cat c1 = new Cat();
co.useCat(c1); //需要一个Cat对象,上面创建Cat对象;猫吃鱼
Cat c2 = co.getCat(); //调用getCat方法,把new Cat()赋值给c2
c2.eat(); //猫吃鱼
}
}
package ceshi;
public abstract class Animal {
public abstract void eat();
}
package ceshi;
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
package ceshi;
public class AnimalOperator {
public void useAnimal(Animal a) { //需要的是Animal的子类对象 Animal a = new Cat();
a.eat();
}
public Animal getAnimal() {
Animal a = new Cat();
return a;
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//创建操作类对象,调用方法
AnimalOperator ao = new AnimalOperator();
Animal a = new Cat();
ao.useAnimal(a);
a.eat();
Animal a2 = ao.getAnimal(); //new Cat()
a2.eat();
}
}
package ceshi;
public interface Jumpping {
void jump();
}
package ceshi;
public class Cat implements Jumpping{
@Override
public void jump() {
System.out.println("猫可以跳高");
}
}
package ceshi;
public class JumppingOperator {
public void useJumpping(Jumpping j) { //需要的是接口实现类,Jumpping j = new Cat();
j.jump();
}
public Jumpping getJumpping() {
Jumpping j = new Cat();
return j;
}
}
package ceshi;
public class Demo {
public static void main(String[] args) {
//创建操作类对象,调用方法
JumppingOperator jo = new JumppingOperator();
Jumpping j = new Cat();
jo.useJumpping(j); //猫可以跳高
Jumpping j2 = jo.getJumpping();
j2.jump(); //猫可以跳高
}
}
public class 类名 {
修饰符 class 类名 {
}
}
package ceshi;
public class Outer {
private int num = 20;
public class Inter {
public void show() {
System.out.println(num); //可以访问外部类私有
}
}
public void method() {
// show(); 报错,不能直接访问需要创建内部类对象
Inter i = new Inter();
i.show();
}
}
格式:外部类名.内部类名 对象名 = new 外部类对象.内部类对象();
范例:Outer.Inter oi = new Outer(). new Inter();
package ceshi;
public class Outer {
private int num = 20;
//测试1
/*public class Inter {
public void show() {
System.out.println(num); //可以访问外部类私有
}
}*/
//一、内部类通常用private修饰;二、通过外部类创建内部类对象,调用内部类方法;三、外部创建外部类对象调用method方法
private class Inter {
public void show() {
System.out.println(num);
}
}
//二、通过外部类创建内部类对象,调用内部类方法
public void method() {
Inter i = new Inter();
i.show();
}
}
package ceshi;
public class InterDemo {
public static void main(String[] args) {
//创建内部类对象,并调用方法
// Inter i = new Inter(); 报错
//测试1
// Outer.Inter oi = new Outer(). new Inter();
// oi.show(); //20
//三、外部创建外部类对象调用method方法
Outer o = new Outer();
o.method(); //20
}
}
package ceshi;
public class Outer {
private int num1 = 10;
public void method() {
int num2 = 20;
//局部内部类
class Inter {
public void show() {
System.out.println(num1);
System.out.println(num2);
}
}
//需要在方法中创建局部内部类对象,调用方法,否则没法访问
Inter i = new Inter();
i.show();
}
}
package ceshi;
public class InterDemo {
public static void main(String[] args) {
Outer o = new Outer();
o.method(); //10
//20
}
}
new 类名或者接口名() {
重写方法
};
package ceshi;
public class Outer {
private int num1 = 10;
public void method() {
//只能调用一次
/*new Inter() {
@Override
public void show() {
System.out.println("匿名内部类");
}
}.show(); //前面为一个对象,调用里面show()方法*/
//赋值给对象可以多次调用
Inter i = new Inter() {
@Override
public void show() {
System.out.println("匿名内部类");
}
};
i.show();
i.show();
}
}
package ceshi;
public interface Inter {
void show();
}
package ceshi;
public class InterDemo {
public static void main(String[] args) {
Outer o = new Outer();
o.method(); //匿名内部类
//匿名内部类
}
}
package ceshi;
public class JumppingDemo {
public static void main(String[] args) {
//创建接口操作类对象,调用method方法
JumppingOperator jo = new JumppingOperator();
//不用创建接口实现类
jo.method(new Jumpping() {
@Override
public void jump() {
System.out.println("猫跳高"); //猫跳高
}
});
jo.method(new Jumpping() {
@Override
public void jump() {
System.out.println("狗跳高"); //狗跳高
}
});
}
}