返回值—权限修饰符—权限修饰符—权限修饰符

返回值
public class test {
public static void main(String[] args) {
//如果你以后看到一个方法要一个类类型那么你就传一个该类的对象
Student student = new Student();
setStudent(student,10);
//匿名对象传参
setStudent(new Student(),100);
//怎么传参:如果你以后看到一个方法,要一个抽象类 类型 ,那么你就传一个该抽象类的 子类对象
Cat cat = new Cat();
setAnimal(cat);
//如果以后你看到一个方法的形参要一个接口类型的参数,你就传该接口的子类对象
Myclass myclass= new Myclass();
setInterface(myclass);
setInterface(new Myclass2());
}
public static void setStudent(Student stu,int num){
stu.show();
}
private static void setAnimal(Aaimal animal){
animal.hehe();
}
private static void setInterface(MyInterface my){
my.haha();
}
}
——————————————————————————————————————
public class MyTest {
public static void main(String[] args) {
Teacher teacher=new Teacher();
Teacher teacher1 = teacher.getTeacher(10);
System.out.println(teacher1.age);//10 10 0
System.out.println(teacher.age);
System.out.println(teacher);
System.out.println(teacher1);
Person p1=new Woker();
p1.a();
Person p = getperson();
p.a();
System.out.println("-----------------------");
MyInterface myInterface1=getMyInterface(new MyClass(),10,20);
MyInterface myInterface=new MyClass();
int r=myInterface.add(10,90);
System.out.println®;
}
//如果以后你看到一个方法的返回值 要一个抽象类类型,那么你就返回一个该类的子类对象
static Person getperson(){
Woker woker=new Woker();
return woker;
}
//如果你以后看到一个方法的返回值要一个接口类型,那么你就返回一个该接口的子类对象
private static MyInterface getMyInterface(MyInterface myInterface,int a,int b){
int add=myInterface.add(a,b);
System.out.println(add);
return new MyClass();
}
}
权限修饰符
本类 同一个包下(子类和无关类) 不同包下(子类) 不同包下(无关类)
private Y
默认 Y Y
protected Y Y Y
public Y Y Y Y
常见的修饰符
A:修饰符:
权限修饰符:private,默认的,protected,public
状态修饰符:static,final
抽象修饰符:abstract
B:类:
权限修饰符:默认修饰符,public
状态修饰符:final
抽象修饰符:abstract
用的最多的就是:public

C:成员变量:
权限修饰符:private,默认的,protected,public
状态修饰符:static,final
用的最多的就是:private
D:构造方法:
权限修饰符:private,默认的,protected,public
用的最多的就是:public
E:成员方法:
权限修饰符:private,默认的,protected,public
状态修饰符:static,final
抽象修饰符:abstract
用的最多的就是:public
F:除此以外的组合规则:
成员变量:public static final
成员方法:public static
public abstract
public final
内部类
内部类概述: 把类定义在其他类的内部,这个类就被称为内部类。
举例:在类A中定义了一个类B,类B就是内部类。
内部类访问特点
a:内部类可以直接访问外部类的成员,包括私有。
b:外部类要访问内部类的成员,必须创建对象。
1. 成员内部类
格式: 外部类名.内部类名 对象名 = 外部类对象.内部类对象;
成员内部类的修饰符:
private 为了保证数据的安全性
static 为了方便访问数据
注意事项: a:静态内部类访问的外部类数据必须用静态修饰。
b: 成员方法可以是静态的也可以是非静态的
成员内部类被静态修饰后的访问方式是:
格式: 外部类名.内部类名 对象名 = new 外部类名.内部类名();
public class Outer {
int num=10;
private String name=“abc”;
//成员内部类
class Inner{
int age=30;
public void innerMethod(){
System.out.println(“我是成员内部类的方法”);
//内部类可以直接访问外部类的成员
System.out.println(num);
//内部类也可以直接访问外部类的私有成员
System.out.println(name);
}
public void innerMethod2(){
System.out.println(“我还是一个内部类的成员方法”);
show();
}
}
private void show1() {
System.out.println(“我是外部类的私有成员方法”);
}
public void show() {
System.out.println(“我是外部类的成员方法”);
}
public void outerMethod(){
//我想在外部类的成员方法中 调用内部类的成员,直接调用不行
Inner inner = new Inner();
System.out.println(inner.age);
inner.innerMethod();
inner.innerMethod2();
}
}

public class test {
public static void main(String[] args) {
//创建成员内部类的对象
// 格式:外部类名.内部类名 对象名 = 外部类对象.内部类对象;
Outer.Inner inner=new Outer().new Inner();
inner.innerMethod();
System.out.println(inner.age);
inner.innerMethod2();
Outer outer=new Outer();
outer.outerMethod();
outer.show();
}
}
2.局部内部类
A: 可以直接访问外部类的成员
B: 可以创建内部类对象,通过对象调用内部类方法,来使用局部内部类功能
C:局部内部类访问局部变量必须用final修饰为什么呢?
因为局部变量会随着方法的调用完毕而消失,这个时候,局部对象并没有立马从堆内存中消失,还要使用那个变量。
为了让数据还能继续被使用,就用fianl修饰,这样,在堆内存里面存储的其实是一个常量值。
在内部类所在的方法中创建内部类对象 然后调用它自己的属性和功能
public class Outer5 {
//局部类内部类:将一个类定义在一个类的局部位置(方法中)
int age=30;
private String name=“abc”;
public void outerMethod(){
//局部内部类
class Inner5{
int num=10;
public void innerMethod(){
System.out.println(“我是局部内部类的成员方法”);
//内部类可以直接访问外部类的成员 包括私有的都可以直接访问
System.out.println(age);
System.out.println(name);
hehe();
}
}
//创建内部类对象
//调用内部类的方法
Inner5 inner5 = new Inner5();
inner5.innerMethod();
}
private void hehe(){
System.out.println(“我是外部类的一个私有方法”);
}
public void haha(){
}
}
public class Test5 {
public static void main(String[] args) {
//可不可以直接创建局部内部类的对象呢?创建不了
Outer5 outer5 = new Outer5();
outer5.outerMethod();
System.out.println("------------------------");
Outer outer=new Outer();
outer.outerMethod();
}
}
匿名本部类
A:匿名内部类: 就是局部内部类的简化写法。
B:前提: 存在一个类或者接口;这里的类可以是具体类也可以是抽象类。
C:格式:
new 类名或者接口名(){
重写方法;
} ;
D:本质是什么呢?
是一个继承了该类或者实现了该接口的子类匿名对象。
E:案例演示:
public class test2 {
public static void main(String[] args) {
new Person(){
@Override
public void eat() {
System.out.println(“111”);
}
@Override
public void sleep() {
System.out.println(“222”);
}
@Override
public void study() {
System.out.println(333);
}
}.eat();
new Person(){
@Override
public void eat() {
System.out.println(“111”);
}
@Override
public void sleep() {
System.out.println(“222”);
}
@Override
public void study() {
System.out.println(333);
}
}.study();
Person p=new Person(){
@Override
public void eat() {
System.out.println(“111”);
}
@Override
public void sleep() {
System.out.println(“222”);
}
@Override
public void study() {
System.out.println(333);
}
};
p.study();
p.eat();
p.sleep();
}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值