java类

一个简单的类:

public class hello {
    //公有属性
    int a = 0;
    String name = "柚子";
    //私有属性
    private String Data = "私房钱";
    //final
    private final String file = "不可变常量";
    public String hello_word()
    {
        return "hello world!";
    }
}

数据 final 修饰基本的数据类型时 必须初始化 否则编译报错

final可以作用于:1.变量:如果用final修饰变量,表示该变量的值不能被修改,

                                即它是一个常量。

                             2.方法:如果用final修饰方法,表示该方法不能被子类覆盖(重写)

                             3.类:如果用final修饰类,表示该类不能被继承

                             4.参数:不能修改参数的值

public void process(final int value) {
    // 在这里不能修改 value 的值
}

访问类中的属性:

1.公有属性:通过类的实例化进行直接访问

2.私有属性:通过类中的方法进行间接访问,否则会报错

 

public class Main
{
    public static void main(String[] args) {
    hello information = new hello();//声明并实例化对象
        System.out.println(information.name);//通过实例化访问公有属性
        System.out.println(information.hello_word());
    }
}
public class hello {
    //公有属性
    int a = 0;
    String name = "柚子";
    //私有属性
    private String Data = "私房钱";
    //final
    private final String file = "不可变常量";
    public String hello_word()
    {
        return Data;
    }
}

类中的私有方法:

Java中对方法的定义存在四种修饰符,分别是public、protected、default、private,作用域分别如下:

调用私有方法:

public class Main
{
    public static void main(String[] args) {
    hello information = new hello();//声明并实例化对象
        System.out.println(information.getHelloMessage());
    }
}
public class hello {
    // 公有属性
    String name = "柚子";

    // 私有方法
    private String helloWord() {
        return "hello world!";
    }

    // 公有方法,用于访问私有方法
    public String getHelloMessage() {
        return helloWord();
    }
}

类中的构造方法:构造方法,就是一种特殊的方法,每当一个类被实例化的时候,就会调用构造方法,而且只有构造方法被调用的时候,对象才会被分配内存空间。

也就是说,每次我们使用new关键字的时候,构造方法至少会被调用一次。

创建构造方法的规则
1.构造方法的名字必须和类名一致
2.构造方法没有返回值,返回的是一个类
3.构造方法不能是抽象的,静态的,最终的,同步的也就是说,他不能通过abstract,static,         final,synchronized关键字修饰

public class Main
{
    public static void main(String[] args) {
    hello information = new hello(1,"柠檬","sss");//声明并实例化对象
        System.out.println(information.hello_word());
    }
}
public class hello {
    //公有属性
    int a = 0;
    String name = "柚子";
    //私有属性
    private String Data = "私房钱";
    //final
    private final String file = "不可变常量";
    public hello(int a,String name,String Data)
    {
        this.a = a;
        this.name = name;
        this.Data = Data;

    }
    public String hello_word()
    {
        return Data;
    }
}

类中的静态方法:

使用类名直接调用

public class Main
{
    public static void main(String[] args) {
    hello.method();//调用静态方法
    }
}
public class hello {

    static void method()
    {
        System.out.println("静态方法");
    }
}

类的继承:

//父类
public class hello {
    //公有属性
    int a = 0;
    String name = "柚子";
    //私有属性
    private String Data = "私房钱";
    //final
   
    public hello(int a,String name,String Data)
    {
        this.a = a;
        this.name = name;
        this.Data = Data;

    }
    public String hello_word() {
        return Data;
    }
}
//子类
class hello_new extends hello
{
    int b;
    String name_new;
    //重写构造方法
    public hello_new(int a, String name, String Data,int b,String name_new) 
    {
        super(a, name, Data);
        this.b = b;
        this.name_new = name_new;
    }
}

注意:一个子类只能继承一个父类

子类中对父类的方法覆写:

public class Main
{
    public static void main(String[] args) {
    hello_new book = new hello_new(1,"柠檬柚子","sss",2,"kkk");
    book.method();
    }
}
//父类
public class hello {
    //公有属性
    int a = 0;
    String name = "柚子";
    //私有属性
    private String Data = "私房钱";
    //final

    public hello(int a,String name,String Data)
    {
        this.a = a;
        this.name = name;
        this.Data = Data;

    }
    public void method()
    {
        System.out.println("这是父类的方法");
    }

}
//子类
class hello_new extends hello
{
    int b;
    String name_new;
    //重写构造方法
    public hello_new(int a, String name, String Data,int b,String name_new)
    {
        super(a, name, Data);
        this.b = b;
        this.name_new = name_new;
    }
    public void method()
    {
        System.out.println("这是子类的方法");
    }
}

父类定义的方法子类可以直接继承:

public class Main
{
    public static void main(String[] args) {
    hello_new book = new hello_new(1,"柠檬柚子","sss",2,"kkk");
    book.method1();
    }
}
//父类
public class hello {
    //公有属性
    int a = 0;
    String name = "柚子";
    //私有属性
    private String Data = "私房钱";
    //final

    public hello(int a,String name,String Data)
    {
        this.a = a;
        this.name = name;
        this.Data = Data;

    }
    public void method()
    {
        System.out.println("这是父类的方法");
    }
    public void method1()
    {
        System.out.println("这是父类的方法");
    }

}
//子类
class hello_new extends hello
{
    int b;
    String name_new;
    //重写构造方法
    public hello_new(int a, String name, String Data,int b,String name_new)
    {
        super(a, name, Data);
        this.b = b;
        this.name_new = name_new;
    }
    public void method()
    {
        System.out.println("这是子类的方法");
    }
}

Annotation

@Override:注释此方法为覆写方法

@Override
    public void method()
    {
        System.out.println("这是子类的方法");
    }

抽象类:

抽象类与接口

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茁@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值