一个简单的类:
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("这是子类的方法");
}
抽象类: