类与对象
- 用来描述客观事物的一个实体,由一组属性和方法构成
- 属性:对象具有的各种特征 每个对象的每个属性都有特定值
- 方法:对象执行的操作
- 类是对象概括、对象的分类,是具有相同方法和属性的一组对象的集合,描述对象拥有的特征和行为
定义类
使用[访问控制符] class 类名
定义类
public class Person {
// 用[访问控制符] 数据类型 变量名 定义属性
String name;
public int age;
// 用 [访问控制符] 返回值类型 方法名 定义方法
public void sleep() {
System.out.println("睡觉");
}
}
构造函数
用于初始化对象
在类中以[访问控制符] 类名(参数列表) { 方法体 }
形式的方法为类的构造函数
如果一个类没有构造函数,则会自动生成一个无参构造函数
使用new 构造函数()
创建对象
class Person{
public int age;
public Person(){
System.out.println("hello");
age = 21;
}
// 一个类中可以有多个构造函数,它们的参数列表不同
public Person(int age) {
this.age = age;
}
public static void main(String[] args){
Person p = new Person(); // hello
System.out.println(p.age); // 21
}
}
方法
用来定义类的行为
[访问修饰符] 返回值类型 类名(参数列表) {方法体}
- 如果方法有返回值,需要return
相应类型的值
- 如果方法无返回值,返回值类型为void
- 调用方法为对象名.方法名()
class Method{
public int method(int a) {
return a;
}
public static void main(String[] args){
Method m = new Method();
m.method(123);
}
}
重载
方法名相同,参数列表不同的两个或多个方法
class Method{
int method(int a) {
return a;
}
int method() {
return 123;
}
public static void main(String[] args){
Method m = new Method();
m.method(123);
m.method();
}
}
面向对象编程
封装
将类的某些信息隐藏在类的内部,不允许外部程序直接访问,而是通过该类提供的方法来实现对隐藏信息的操作和访问
增加了代码的安全性
用private
将类的成员声明为私有的
在类的外部无法直接访问私有成员,可以通过方法间接访问
class Person {
private int age;
public void setAge(int age) {
this.age = age;
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.age = 12; // error
p.setAge(12);
}
}
继承
使用extends
继承
子类拥有父类的属性和方法
子类使用super
访问父类protected
以上级别的成员
class Parent {
public void method() {
}
}
class Child extends Parent {
void meth() {
super.method();
}
}
public class Test{
public static void main(String[] args){
Child c = new Child();
c.method();
}
}
访问修饰符
private
仅在本类中可访问- 默认 在同包中可访问
protected
子类可访问public
随处可访问
重写
子类中与父类拥有相同方法名,返回值,参数类型的方法
访问修饰符不能比父类更严格
不能重写构造函数
class Parent {
public void method() {
System.out.println("aaa");
}
}
class Child extends Parent {
public void method() {
System.out.println("bbb");
}
}
public class Test{
public static void main(String[] args){
Child c = new Child();
c.method(); // bbb
}
}
抽象
使用abstract
修饰符使类和方法变为抽象的
抽象类不能实例化,只能被继承
抽象方法只能存在于抽象类中
抽象方法没有方法体,子类必须实现该方法,除非子类也是抽象类
abstract class Animal{
abstract void play();
}
class Dog extends Animal{
void play(){
}
}
public class Test{
public static void main(String[] args){
Animal a = new Animal(); // error
Dog d = new Dog();
}
}
常量
使用final
修饰符声明常量类和常量成员
final
修饰的类不能被继承,方法不能被重写,变量不能被修改
final class FinalClass{
final int a;
final void method() {}
}
class Child extends FinalClass { // error
void method() {} //error
}
public class Test{
public static void main(String[] args){
final int a = 1;
a = 2; // error
}
}
静态
使用static
声明一个静态方法或属性
类的所有实例共用同一个静态属性
static
成员可以直接通过类名.成员名
访问而无需实例化
public class Test{
static int a = 1;
public static void main(String[] args){
Test t1 = new Test();
Test t2 = new Test();
System.out.println(t1.a); // 1
System.out.println(t2.a); // 1
System.out.println(Test.a); // 1
t1.a++;
System.out.println(Test.a); // 2
}
}
接口
使用interface
定义接口
接口中的方法默认为public abstract
的
接口不可直接实例化
一个类只能继承一个类,但能实现多个接口
使用implements
实现接口,多个接口间用逗号隔开
接口中的属性全是public static final
的
实现类必须实现接口的所有方法,除非实现类是抽象的
接口可以实现其他接口
public interface Intfc{
void method1();
int sta = 10;
// java8允许为接口的方法定义默认实现
default void method2(){
}
}
class IntfcClass implements Intfc{
void method1(){}
}