一、面向对象的特点
目录
1.1. 封装
- 对象的行为和属性是密不可分的整体
- 将不希望外界知道的信息隐藏起来
1.2. 继承
借助已存在的类的定义建立新类,新类可以增加新的数据或新的功能,也可以使用父类的功能,但不能选择性地继承父类
1.3. 多态
一个类中定义的方法和属性被其它类继承之后,可以具有不同的数据类型和(方法)表现形式
二、类与对象
2.1 类
class 类名 {
成员变量;
成员方法;
}
class Student {
String sName;
int sAge;
String sSex;
void introduce() {
System.out.println("你好,我是" + sName);
}
}
2.2 创建使用对象
2.2.1. 对象的创建
类名 对象名;
对象名 = new 类名();
类名 对象名 = new 类名();
public class Demo01 {
public static void main(String[] args) {
Teacher t1 = new Teacher();
}
}
class Teacher {
String tName;
int tAge;
String tSex;
void introduce() {
System.out.println("你好,我是" + tName);
}
}
2.2.2. 对象的使用
对象名称.属性名
对象名称.方法名
public class Demo02 {
public static void main(String[] args) {
Teacher t1 = new Teacher();
t1.tName = "李老师";
t1.introduce(); // 你好,我是李老师
Teacher t2 = new Teacher();
t2.tName = "王老师";
t2.introduce(); // 你好,我是王老师
}
}
class Teacher {
String tName;
int tAge;
String tSex;
void introduce(){
System.out.println("你好,我是" + tName);
}
}
2.2.3. 对象引用传递
public class Demo03 {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1 = s2;
s1.sName = "小李";
s1.sAge = 20;
s1.introduce(); // 你好,我是小李
s2.introduce(); // 你好,我是小李
s2.sName = "小王";
s1.introduce(); // 你好,我是小王
s2.introduce(); // 你好,我是小王
}
}
class Student {
String sName;
int sAge;
String sSex;
void introduce(){
System.out.println("你好,我是" + sName);
}
}
类属于引用类型,在语句“s1 = s2”中,将s2内存指向s1内存(可以理解为 C 的指针),所以对一个对象的改变也会反映到另一个对象
2.2.4. 访问控制
针对类,成员方法以及属性,java提供类4中访问控制权限
- private - 私有访问权限,只能修饰类的属性和方法;表示只能在本类中进行访问
- friendly - 若无另外的权限声明,即为默认;只能在本包的类中进行访问,不能被其他包的类访问
- protected - 受保护的访问权限;只能被本包以及其他包的子类访问
- public - 公共访问权限,可以被所有类访问
2.3 封装
防止本类的代码和数据被外部程序访问
- 不同类型的数据取值范围是预设的
- 生产中的一些数据在取值范围内是非法的
- int的范围[-2147483648~2147483647]
- 年龄的范围:0~160
- 如果年龄赋值20000,语法和合法的,生产场景中是非法的
- 所以需要限制属性的赋值
实现
将类中的属性与方法私有化
public class Demo04 {
public static void main(String[] args) {
Student s1 = new Student();
s1.introduce(); //你好,我是null
s1.setsName("小李");
s1.setsAge(20);
s1.introduce(); //你好,我是小李
}
}
class Student {
private String sName;
private int sAge;
void introduce() {
System.out.println("你好,我是" + sName);
}
/*
私有方法与私有属性无法在类外访问
所以需要提供一组方法
setXxx:设置数据【修改】
getXxx:获取数据【读取】
*/
String getsName() {
return this.sName;
}
void setsName(String str) {
this.sName = str;
}
String getsAge() {
return this.sName;
}
void setsAge(int a) {
this.sAge = a;
}
}
2.4 构造方法
- 构造方法名称要与类名一致
- 构造方法前不能有返回值类型声明
- 构造方法不能有返回值,但可以单独写return语句作文构造方法的结束
public class Demo05 {
public static void main(String[] args) {
Teacher t1 = new Teacher("李老师", 38);
t1.introduce();
}
}
class Teacher {
String tName;
int tAge;
public Teacher(String str, int a) {
tName = str;
tAge = a;
}
void introduce() {
System.out.println("你好,我是" + tName + ", " + tAge + "岁");
}
}
构造方法的重载
public class Demo06 {
public static void main(String[] args) {
Student s1 = new Student("小李");
s1.introduce(); //你好,我是小李, 0 岁
Student s2 = new Student("小王", 29);
s2.introduce(); //你好,我是小王, 29 岁
}
}
class Student {
String sName;
int sAge;
void introduce(){
System.out.println("你好,我是" + sName + ", " + sAge + " 岁");
}
public Student(String str) {
this.sName = str;
}
public Student(String str, int a) {
this.sName = str;
this.sAge = a;
}
}
构造代码块
直接定义在类中的代码块(与构造方法同级)
public class Demo07 {
public static void main(String[] args) {
Student s1 = new Student();
/*
运行结果:
构造代码块
构造方法
*/
}
}
class Student {
String sName;
// 构造代码块
{
System.out.println("构造代码块");
}
public Student(){
System.out.println("构造方法");
}
}
2.5 this关键字
this关键字可以调用本类的属性与方法(包括构造方法)
- 调用实例变量:this.实例变量
- 调用实例方法:this.实例方法(参数)
- 调用其他构造器:this(参数)
//this调用构造方法
public class Demo08 {
public static void main(String[] args) {
Student s1 = new Student("小李");
s1.introduce();
/*
运行结果:
无参构造方法
你好,我是小李
*/
}
}
class Student {
String sName;
int sAge;
void introduce(){
System.out.println("你好,我是" + sName);
}
public Student(){
System.out.println("无参构造方法");
}
public Student(String str) {
this(); //调用无参构造方法
this.sName = str;
}
}
三、静态类、静态方法与静态变量
static是静态成员变量和成员方法的修饰符,可以修饰内嵌类,但不能用于修饰外部类
java中的静态概念用于在java中创建类级别的变量(静态方法、静态变量),它们自身相当于类,而不是类的实例
static修饰的内容(类、方法、变量)为所有成员所共享(公共资源),或者说当某个类的实例修改了该静态变量,且其修改值为其它所有实例所见
注意:在java中,若将外部类声明为static,程序编译不通过
3.1 静态变量
一个类中的静态变量,属于类,不属于类的对象或者实例
静态变量与所有的对象实例共享,因此不具备线程安全性
若静态变量未被私有化,可以用“类名.变量名”的方式来使用
public class Demo09 {
public static void main(String[] args) {
Student s1 = new Student("小李", 23);
Student s2 = new Student("小王", 24);
s1.printInfo(); // 我是小李,23岁,毕业于X大学
s2.printInfo(); // 我是小王,24岁,毕业于X大学
Student.school = "Y大学";
s1.printInfo(); // 我是小李,23岁,毕业于Y大学
s2.printInfo(); // 我是小王,24岁,毕业于Y大学
}
}
class Student {
String sName;
int sAge;
static String school = "X大学";
public Student(String str, int a) {
this.sName = str;
this.sAge = a;
}
public void printInfo(){
System.out.println("我是" + sName + "," + sAge + "岁,毕业于" + school);
}
}
3.2 静态方法
与静态变量一样,静态方法属于类而非实例
在静态方法中,只能访问静态成员,静态内部类
一个静态方法只能使用静态变量和调用静态方法。通常静态方法通常用于希望不创建实例,通过类名直接调用方法的情况
注意:main()方法就是静态方法
public class Demo10 {
public static void main(String[] args) {
Student s1 = new Student("小李", 23);
Student s2 = new Student("小王", 24);
s1.printInfo(); // 我是小李,23岁,毕业于X大学
s2.printInfo(); // 我是小王,24岁,毕业于X大学
Student.setSchool(); //通过类名直接调用静态方法
s1.printInfo(); // 我是小李,23岁,毕业于Y大学
s2.printInfo(); // 我是小王,24岁,毕业于Y大学
}
}
class Student {
String sName;
int sAge;
static String school = "X大学";
public Student(String str, int a) {
this.sName = str;
this.sAge = a;
}
public static void setSchool() {
Student.school = "Y大学";
}
public void printInfo(){
System.out.println("我是" + sName + "," + sAge + "岁,毕业于" + school);
}
}
3.3 静态类
java可以嵌套使用静态类,但是静态类不能成为外部类
**注意:**静态类用于当外部类需要使用内部类,而内部类无需外部类资源,且内部类可以单独创建的时候
public class Demo11 {
public static void main(String[] args) {
// 实例化外部类
Outer o1 = new Outer();
// 在使用内部类时需要先实例化外部类
Outer.InnerClass i1 = o1.new InnerClass();
//使用静态内部类时不需要实例化外部类
Outer.StaticInnerClass si1 = new Outer.StaticInnerClass();
// 访问外部类静态变量
System.out.println(Outer.s1);
/*
输出:
1
*/
i1.accessInnerClass();
/*
输出:
1
2
普通内部类方法
*/
si1.accessStaticInnerClass(); //访问静态方法
/*
输出:
1
静态方法
*/
}
}
class Outer{
public static int s1 = 1;
public int s2 = 2;
class InnerClass{
// 可以定义静态与非静态属性
public static int age = 22;
public int num = 35;
public void accessInnerClass() {
// 可以访问外部类静态与非静态成员
System.out.println(s1);
System.out.println(s2);
System.out.println("普通内部类方法");
}
}
// 静态内部类
public static class StaticInnerClass {
// 可以定义静态与非静态属性
public static int age = 22;
public int num = 35;
public void accessStaticInnerClass() {
// 只能访问外部类的静态成员
System.out.println(s1);
System.out.println("静态方法");
}
}
}
3.4 静态代码块
- 类加载器加载对象时,要执行的一组语句
- 用于初始化静态变量
- 静态块只会在类加载到内存中的时候执行一次
注意:静态代码块通常用于类加载的时候创建静态资源
public class Demo12 {
public static void main(String[] args) {
Example e1 = new Example();
Example e2 = new Example();
Example e3 = new Example();
/*
输出结果:
此为静态代码块
此为构造代码块
此为构造代码块
此为构造代码块
*/
}
}
class Example{
{
System.out.println("此为构造代码块");
}
static{
System.out.println("此为静态代码块");
}
}