目录
一、面向对象的初步认识
1.1 什么是面向对象
1.2 面向对象与面向过程
常说C语言是面向过程,Java是面向对象。
那什么是面向对象,什么是面向过程呢?
简单举个例子,现在我要将一个大象装入冰箱里。如何思考这个问题
那么大部分情况下,首先想到的就是,怎么打开冰箱门,怎么装进去,怎么又关上冰箱门。
这就是面向过程。
而我还可以这样想
整个过程是冰箱、人、大象这三个对象交互完成的,具体这个冰箱门怎么打开的,大象怎么关进去
的,冰箱门又是怎么关上的这些过程不关注。全程都是由三个对象交互完成的
这就是面向对象。
二、类的定义与使用
2.1 简单认识类
2.2 类的定义格式
public class Person{
public String name;
public String sex;
public String edbd;//Educational background
public String occupation;
public String interest;
public int age;
public float height;
public void eat(){
System.out.println(name+"正在吃饭");
}
public void sleep(){
System.out.println(name+"正在睡觉");
}
public void walk(){
System.out.println(name+"正在散步");
}
}
三、类的实例化
3.1 什么是实例化
注意事项
new 关键字用于创建一个对象的实例.
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.age = 18;
person.name = "张三";
person.edbd = "本科";
person.height = 1.75f;
person.eat();
person.sleep();
}
}
3.2 类和对象的说明
四、this引用
4.1 为什么要有this引用
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public void setperson1(String name,int age){
name = name;
age = age;
}
public void printperson(){
System.out.println(name);
System.out.println(age);
}
}
public class Javacode{
public static void main(String[] args) {
Person person = new Person();
person.setperson1("卢本伟",18);
person.printperson();
person.setperson1("卢本伟",18);
person.printperson();
}
}
形参名与成员变量名相同
那函数体中到底是谁给谁赋值?成员变量给成员变量?参数给参数?参数给成员变量?成员变量参
数?
两个对象都在调用setperson1和printperson函数,但是这两个函数中没有任何有关对象说
明,setperson和 printperson函数如何知道打印的是那个对象的数据呢?
4.2 什么是this引用
这里可以认为这里有一个隐藏的参数
this引用指向当前对象(成员方法运行时调用该成员方法的对象),在成员方法中所有成员变量的操
作,都是通过该引用去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,编译器自
动完成。
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public void setperson1(String name,int age){
name = name;
age = age;
}
public void setperson2(String name,int age){
this.name = name;
this.age = age;
}
public void printperson(){
System.out.println(this.name);
System.out.println(this.age);
}
}
public class Javacode{
public static void main(String[] args) {
Person person = new Person();
person.setperson2("卢本伟",18);
person.printperson();
person.setperson2("666",20);
person.printperson();
}
}
在进行成员方法的定义时,若要调用成员变量,都带上this,这样赋值就不会错。
同时,在成员方法中,可以使用this来调用另一个成员方法
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public void setperson1(String name,int age){
name = name;
age = age;
}
public void setperson2(String name,int age){
this.name = name;
this.age = age;
}
public void printperson(){
this.eat();
this.sleep();
System.out.println(this.name);
System.out.println(this.age);
}
}
public class Javacode{
public static void main(String[] args) {
Person person = new Person();
person.setperson2("卢本伟",18);
person.printperson();
person.setperson2("666",20);
}
4.3 this引用的特性
五、对象的构造及初始化
5.1 初始化对象的成员变量
5.1.1 默认初始化
在使用局部变量时,不初始化编译器就会报错
但在使用成员变量就不初始化就可以使用
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public void setperson(String name,int age){
this.name = name;
this.age = age;
}
public void printperson(){
System.out.println(name);
System.out.println(age);
}
}
public class Javacode4_28_2 {
public static void main(String[] args) {
Person person = new Person();
person.printperson();
}
}
在未对成员变量进行初始化时,Java会自动为他们进行默认初始化
数据类型 | 默认值 |
int | 0 |
float | 0.0f |
double | 0.0 |
char | '\u0000' |
boolean | false |
引用类型(如String) | null |
5.1.2 就地初始化
即在类中就对成员变量进行初始化
class Person{
public String name ="小明" ;
public int age = 12;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public void setperson(String name,int age){
this.name = name;
this.age = age;
}
public void printperson(){
System.out.println(name);
System.out.println(age);
}
}
public class Javacode4_28_2 {
public static void main(String[] args) {
Person person = new Person();
person.printperson();
}
}
5.2 构造方法
5.2.1 概念
构造方法(也称为构造器)是一个特殊的成员方法,名字必须与类名相同,在创建对象时,由编译器
自动调用,并且在整个对象的生命周期内只调用一次。
5.2.2 特性
1.名字必须与类名相同
2.没有返回值类型,设置为void也不行
3.创建对象时由编译器自动调用,并且在对象的生命周期内只调用一次(相当于人的出生,每个人
只能出生一次)
4.构造方法可以重载(用户根据自己的需求提供不同参数的构造方法)
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public Person(){
this.name = "张三";
this.age = 6;
}
public void print(){
System.out.println(this.name +" "+this.age);
}
}
public class Javacode {
public static void main(String[] args) {
Person person = new Person();
person.print();
}
}
这里进行一下验证
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public Person(){
System.out.println("调用构造方法");
}
public void print(){
System.out.println(this.name +" "+this.age);
}
}
public class Javacode {
public static void main(String[] args) {
Person person = new Person();
person.print();
}
}
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public Person(){
this.name = "张三";
this.age = 6;
System.out.println("调用构造方法");
}
public void print(){
System.out.println(this.name +" "+this.age);
}
}
public class Javacode {
public static void main(String[] args) {
Person person = new Person();
person.print();
}
}
可以看到,是在创建对象的时候就已经调用这个方法了
那么此时看回默认初始化,在没有手动定义一个构造方法的情况下,那么编译器会自动生成一份默
认的构造方法,生成的默认构造方法一定是无参的
这里可以看到,再手动定义一个含参数构造方法后,main函数中就报错了,原因是没有传参
这说明编译器只会在没有手动定义的情况下生成一个无参的构造方法
同时说明,实例化一个对象的时候,至少是有两步是很关键的:
1.为对象分配内存
2.调用合适的构造方法
注意:构造方法的作用就是对对象中的成员进行初始化,并不负责给对象开辟空间。
5.2.3 构造方法中的this引用
在构造方法中,可以通过this来调用其他构造方法
class Person{
public String name ;
public int age ;
public void sleep(){
System.out.println(this.name+"正在睡觉!");
}
public void eat(){
System.out.println(this.name+"正在吃饭!");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name) {
this.name = name;
}
public Person() {
this("卢本伟",18);
}
public void print(){
System.out.println(this.name +" "+this.age);
}
}
public class Javacode4_28_3 {
public static void main(String[] args) {
Person person = new Person();
person.print();
}
}
需要注意的是,在构造方法中使用this调用其他方法的时候,this必须是该构造方法的第一句语句
同时,不能形成环(不能自己调用自己)
在一些书和博客中,有这样的话
this代表当前对象,这句话是有歧义的
在上文中,说到了这两步,而这两步是对象在被初始化出来的过程中进行的(也就是说这时候对象
还没有完成初始化)。
所以这里更加严谨的说法应是
this代表当前对象的引用
六、封装
6.1封装
6.2 访问修饰限定符
范围 | private | default | protected | public | |
1 | 同一包中同一类 | √ | √ | √ | √ |
2 | 同一包中不同类 | √ | √ | √ | |
3 | 不同包中的子类 | √ | √ | ||
4 | 不同包中非子类 | √ |
6.3 封装扩展包
6.3.1 导入包中的类
Java 中已经提供了很多现成的类供我们使用. 例如Scanner类
可以使用java.util.Scanner导入java.util这个包中的Scanner
public class Test2 {
public static void main(String[] args) {
java.util.Scanner scanner = new Scanner(System.in);
Person person = new Person();
}
}
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Person person = new Person();
}
}
6.3.2 自定义包
基本规则
在文件的最上方加上一个 package 语句指定该代码在哪个包中.
6.3.3 包的访问权限控制
类中成员变量没有任何访问修饰限定符的时候,认为该成员变量是包访问权限
此时可以实例化对象,但是不能给成员变量初始化(这是因为不在同一个包中)
这里是在同一个包中,所以可以调用
6.3.4 常见的包
七、static成员
7.1 再说前文人类
public class Main {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
}
}
7.2 static修饰成员变量
这里看到,出现了黄色警告
打个断点debug一下
可以看到person1当中并没有edbd这个属性
这说明不需要通过对象引用访问edbd,这个属性不依赖对象
直接通过类名访问就可以了
public class Person{
public String name;
public String sex;
public static String edbd = "本科";//Educational background
public String occupation;
public String interest;
public int age;
public float height;
public void eat(){
System.out.println(name+"正在吃饭");
}
public void sleep(){
System.out.println(name+"正在睡觉");
}
public void walk(){
System.out.println(name+"正在散步");
}
public static void func(){
System.out.println("静态方法!");
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
// System.out.println(person1.edbd);
// System.out.println(person2.edbd);
// System.out.println(person3.edbd);
System.out.println(Person.edbd);
Person.func();
}
}
7.3 static修饰成员方法
7.4 static成员变量初始化
注意:静态成员变量一般不会放在构造方法中来初始化,构造方法中初始化的是与对象相关的实例
属性
7.4.1 就地初始化
在定义时就进行初始化
7.4.2 静态代码块初始化
什么是代码块初始化,继续往下看
八、代码块
8.1 代码块概念及分类
8.2 普通代码块
public class Main {
public static void main(String[] args) {
{
int a = 10;
System.out.println(a);
}
int x = 100;
System.out.println(x);
}
8.3 构造代码块儿
构造块:定义在类中的代码块(不加修饰符)。也叫:实例代码块。构造代码块一般用于初始化实例
成员变量。
public class Person{
public String name;
public String sex;
public String edbd ;//Educational background
public String occupation;
public String interest;
public int age;
public float height;
{
this.name = "张三";
this.age = 18;
this.sex = "male";
System.out.println("666");
}
public void show(){
System.out.println(this.name+" "+this.age+" "+this.sex);
}
public void eat(){
System.out.println(name+"正在吃饭");
}
public void sleep(){
System.out.println(name+"正在睡觉");
}
public void walk(){
System.out.println(name+"正在散步");
}
public static void func(){
System.out.println("静态方法!");
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
person.show();
}
8.4 静态代码块
public class Person{
public String name;
public String sex;
public static String edbd ;//Educational background
public static String occupation;
public String interest;
public int age;
public float height;
{
this.name = "张三";
this.age = 18;
this.sex = "male";
System.out.println("666");
// 构造代码块
}
static {
edbd = "本科";
occupation = "码农";
System.out.println("123456");
// 静态代码块
}
public void show(){
System.out.println(this.name+" "+this.age+" "+this.sex);
}
public void eat(){
System.out.println(name+"正在吃饭");
}
public void sleep(){
System.out.println(name+"正在睡觉");
}
public void walk(){
System.out.println(name+"正在散步");
}
public static void func(){
System.out.println("静态方法!");
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
}
这里可以看到静态代码块中的方法在构造代码块前出现,且仅出现了一次
注意事项
九、对象的打印
public class Person{
public String name;
public String sex;
public static String edbd ;//Educational background
public static String occupation;
public String interest;
public int age;
public float height;
public Person(String name, String sex, String interest, int age, float height) {
this.name = name;
this.sex = sex;
this.interest = interest;
this.age = age;
this.height = height;
}
public void show(){
System.out.println(this.name+" "+this.age+" "+this.sex);
}
public void eat(){
System.out.println(name+"正在吃饭");
}
public void sleep(){
System.out.println(name+"正在睡觉");
}
public void walk(){
System.out.println(name+"正在散步");
}
public static void func(){
System.out.println("静态方法!");
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("张三","male","basketball",18,1.75f);
System.out.println(person1);
}
}
public class Person{
public String name;
public String sex;
public static String edbd ;//Educational background
public static String occupation;
public String interest;
public int age;
public float height;
public Person(String name, String sex, String interest, int age, float height) {
this.name = name;
this.sex = sex;
this.interest = interest;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", interest='" + interest + '\'' +
", age=" + age +
", height=" + height +
'}';
}
public void show(){
System.out.println(this.name+" "+this.age+" "+this.sex);
}
public void eat(){
System.out.println(name+"正在吃饭");
}
public void sleep(){
System.out.println(name+"正在睡觉");
}
public void walk(){
System.out.println(name+"正在散步");
}
public static void func(){
System.out.println("静态方法!");
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person("张三","male","basketball",18,1.75f);
System.out.println(person1);
}