惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
About on SuperTechFans
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
G
Google Developers Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Latest news
Latest news
I
Intezer
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Threatpost
博客园 - 【当耐特】
S
Schneier on Security
P
Privacy International News Feed
G
GRAHAM CLULEY
T
Tenable Blog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
博客园 - Franky
Engineering at Meta
Engineering at Meta
美团技术团队
S
Secure Thoughts
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 远洪

大模型常用术语 windows 使用sshAgent 加载秘钥 再次认识java反射 再次认识java注解 再次认识java泛型 java类中的成员变量,静态变量与局部变量 再谈java枚举enum 使用CCProxy让手机访问电脑能访问的网址 playwright启动后报错net::ERR_CERT_COMMON_NAME_INVALID 解决方法 debian 或ubuntu安装使用tigervnc python 实例属性、类属性、实例方法、类方法、静态方法 python面向对象封装,私有变量 docker compose使用 docker 自定义网络 Dockerfile 使用 golang进程(主线程)与协程 go语言多态中的类型断言 java中的多态与golang中的多态 golang 定义接口
java中类的分类
远洪 · 2024-10-15 · via 博客园 - 远洪

标准本的类

package org.example.classTest;
// 一个标准类
class Car{
    private String brand;
    public Car(String brand){
        this.brand = brand;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
}

public class StandardClassTest {
    public static void main(String[] args) {
        Car jili = new Car("吉利");
        System.out.println(jili.getBrand());
    }
}

内部类:如果一个类定义在另一个内的内部,这个类就是内部类

场景:当一个类的内部,包含了一个完整的事物,且这个事物没必要单独设计时,就可以把这个事物设计成内部类。

成员内部类

 成员内部类案例:

package org.example.classTest;

class Outer{
    int number = 10;
    public void show(){
        System.out.println(number);
    }
    public class Inner {
        int number = 20;
        public void show(){
            System.out.println(number);
        }
    }
}

public class InnerClassTest {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.show();
        // 成员内部类的实例化 外部类名.内部类名  对象名 = new外 部类名.内部类名().new 内部类名()
        Outer.Inner inner = new Outer().new Inner();
        inner.show();
    }
}

静态内部类

有static 修饰的内部类

package org.example.classTest;

class Outer2{
    static class Inner{
        int num = 10;
        public void show(){
            System.out.println(num);
        }
    }
}

public class StaticInnerClassTest {
    public static void main(String[] args) {
        // 外部类名.内部类名 对象名 = new 外部类名.内部类名()
        Outer2.Inner inner = new Outer2.Inner();
        inner.show();
    }
}

局部内部类

 局部类内部类是定义在类的方法中、代码块中,构造器中的类;这是一种鸡肋写法,实际代码一般不这么写。

package org.example.classTest;

class Outer3{
    public void test(){
        // 局部类
        class A{

        }
    }
    // 局部抽象类
    abstract class B{

    }
    // 局部接口
    interface C{

    }
}

public class PartInnerClassTest {
    public static void main(String[] args) {

    }
}

匿名内部类(重点)

匿名内部类就是一种特殊的局部内部类,所谓匿名,指的是程序员不需要为这个类声明名字。

package org.example.classTest;

public class AnonymousInnerClassTest {
    public static void main(String[] args) {
        showArts(new Arts() {
            @Override
            public void sing() {
                System.out.println("唱歌");
            }
        });
    }

    public static void showArts(Arts art){
        art.sing();
    }
}

interface Arts{
    public void sing();
}