1、什么是面向对象
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P60
2、回顾方法的定义、调用
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P61 【狂神说Java】Java零基础学习视频通俗易懂P62
方法的定义
package com.oop;
import java.io.IOException;
//Demo01类
public class Demo01 {
//Main方法(每个类只有一个)
public static void main(String[] args) {
}
/*
修饰符 返回值类型 方法名(…){
方法体
return 返回值;
}
*/
public String sayHello(){
return "Say Hello";
}
public int max(int a,int b){
return a>b ? a : b ; //三元运算符
}
//抛出异常
//数组下标越界:Arrayindexoutofbounds
public void readFile(String file) throws IOException{
}
}
方法的调用1 调用Student类的方法
package com.oop;
public class Demo02{
//静态方法 static
//动态方法
public static void main(String[] args) {
//静态方法调用
Student.say();
//实例化这个类 new
//对象类型 对象名 = 对象值;
Student student = new Student();
student.play();
}
}
Student类
package com.oop;
public class Demo02{
//静态方法 static
//动态方法
public static void main(String[] args) {
//静态方法调用
Student.say();
//实例化这个类 new
//对象类型 对象名 = 对象值;
Student student = new Student();
student.play();
}
}
方法的调用2
package com.oop;
public class Demo03 {
public static void main(String[] args) {
}
// 静态方法--和类一起加载的,所以不能直接调用b()方法
public static void a(){
//b();
}
// 动态方法--类实例化 之后才存在
public void b(){
}
}
方法调用3
package com.oop;
public class Demo04 {
public static void main(String[] args) {
//实际参数 和 形式参数的类型要对应
int add = Demo04.add(1, 2);
System.out.println(add);
}
public static int add(int a, int b){
return a+b;
}
}
方法调用4 -----值传递
package com.oop;
//值传递
public class Demo05 {
public static void main(String[] args) {
int a =1;
System.out.println(a);// 结果1
Demo05.change(a);
System.out.println(a);// 结果1
}
public static void change(int a){
a=10;
}
}
方法调用5 -----引用传递
package com.oop;
import java.sql.SQLOutput;
//引用传递: 对象,本质还是值传递
public class Demo06 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name); //null
Demo06.change(person);
System.out.println(person.name); //单一云
}
public static void change(Person person){
//Person是一个对象:指向 ---> Person person = new Person();这是一个具体的人,可以改变属性!
person.name = "单一云";
}
}
//定义一个Person类,有一个属性:
class Person{
String name;
}
3、类和对象的创建
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P63
Student类 :一个模板,相当于一张 空白 的学生表格,里面包括姓名,年龄,身份证等等都有
package com.oop.Demo02;
//学生类
public class Student {
//属性:字段
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"在学习");
}
}
Application 类 :引用到Student类,在空白的学生表格里填下每个人的 具体信息。
package com.oop.Demo02;//一个项目只存在一个main方法public class Application { public static void main(String[] args) { //类:抽象的,实例化 //类实例化后会返回一个自己的对象 //student对象就是一个Student类的具体实例 Student xiaoming = new Student(); Student xh = new Student(); xiaoming.name = "小明"; xiaoming.age = 5; xh.name = "小红"; xh.age = 6; System.out.println(xiaoming.name); System.out.println(xiaoming.age); System.out.println(xh.name); System.out.println(xh.age); }}
4、构造器的详解
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P64
代码
package com.oop.Demo02;//java ---> classpublic class Person { //一个类即使什么都不写,它也会存在一个方法 //显示的定义构造器 String name; //实例化初始值 //1、使用new关键字,本质是在调用构造器 //2、构造器用来初始化值 public Person(){ this.name = "一云"; } //有参构造:一旦定义了有参构造,无参就把必须显示定义 public Person(String name){ this.name = name; } // alt + insert 构造器快捷键(有参、无参)}/*public static void main(String[] args) { //实例化一个对象 Person person = new Person(); System.out.println(person.name); } 构造器: 1、和类名相同 2、没有返回值 作用: 1、new的本质在调用构造方法 2、初始化对象的值 注意点: 1、定义有参构造之后,如果想使用无参构造,显示的定义一个无参构造 Alt + insert this. = */
5、创建对象内容分析
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P65
Pet类
package com.oop.demo03;public class Pet {
public String name;
public int age; //无参构造
public void shout(){
System.out.println("叫了一声");
}
}
Application类
package com.oop;
import com.oop.demo03.Pet;
public class Application {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name = "旺财";
dog.age = 3;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
}
}
6、简单小结类与对象
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P66
要点
1、类与对象 类是一个模板:抽象,对象是一个具体的实例
2、方法 定义、调用!
3、对应的引用 引用类型: 基本类型(8) 对象是通过引用来操作的:栈--->堆
4、属性:字段Field 成员变量 默认初始值: 数字: 0 0.0 char: u0000 Boolean: false 引用: null 修饰符 属性类型 属性名 = 属性值
5、对象的创建和使用 --必须使用new关键字创建对象,构造器 Person yiyun = new Person(); --对象的属性 yiyun.name --对象的方法 yiyun.sleep()
6、类 静态的属性 属性 动态的行为 方法
7、封装详解
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P67
Student类
package com.oop.demo04;//类
private:私有/*
1、提高程序的安全性,保护数据
2、隐藏代码的实现细节
3、统一接口
4、系统可维护性增加 */
public class Student {//属性私有
private String name; //名字
private int id; //学号
private char sex; //性别
private int age; //年龄 //提供一些可以操作这些属性的方法! //提供一些 public 的 get/set 方法 //get 获得这个数据
public String getName(){
return this.name;
} //set 给这个数据设置值
public void setName(String name){
this.name = name;
} //快捷键 Alt + insert
public int getAge() {
return age;
}
public void setAge(int age) {
if (age>120 || age<0){
this.age = 3;
}else{
this.age = age;
}
}
}
Application类
package com.oop;
import com.oop.demo04.Student;
public class Application {
public static void main(String[] args) {
Student S1 = new Student();
S1.setName("一云");
System.out.println(S1.getName());
S1.setAge(999); //不合法
System.out.println(S1.getAge());
}
}
8、什么是继承
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P68
Person类
package com.oop.demo05;// 在Java中,所有的类,都默认直接或者间接继承 Object 类// Person 人 :父类public class Person /* extends Object */{ //public 公共 //protected //default 默认 //private 私有 private int money = 10_0000_0000; public void say(){ System.out.println("说了一句话"); } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; }}
Student 类
package com.oop.demo05;//学生 is 人 :派生类,子类//子类继承了父类,就会拥有父类的全部方法public class Student extends Person{ // Ctrl + H}
Teacher 类
package com.oop.demo05;//Teacher is 人 :派生类,子类public class Teacher extends Person{}
Application 类
package com.oop.demo05;import com.oop.demo05.Student;public class Application { public static void main(String[] args) { Student student = new Student(); student.say(); System.out.println(student.getMoney()); }}
9、Super讲解
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P69
Person类
package com.oop.demo06;// 在Java中,所有的类,都默认直接或者间接继承Object// Person 人 :父类public class Person { public Person() { System.out.println("Person无参构造执行了"); } protected String name = "一云"; public void print(){ System.out.println("Person"); }}
Student类
package com.oop.demo06;//学生 is 人 :派生类,子类//子类继承了父类,就会拥有父类的全部方法public class Student extends Person { public Student() { //隐藏代码:调用了父类的无参构造 super(); //调用父类的构造器,必须要在子类构造器的第一行 //如果父类的是有参,子类调用必须是有参super(name)或者建 有参构造 System.out.println("Student无参构造执行了"); } private String name = "二稀"; public void print(){ System.out.println("Student"); } public void test1(){ print(); //Student this.print(); //Student super.print(); //Person } public void test(String name){ System.out.println(name); //单 System.out.println(this.name); //二烯 System.out.println(super.name); //一云 }}
Application 类
package com.oop.demo06;//测试类public class Application { public static void main(String[] args) { Student student = new Student(); //student.test("单"); //student.test1(); }}
note 笔记
Super注意点 1、super 调用父类的构造方法,必须在构造方法的第一个 2、super 必须只能出现在子类的方法或构造方法中! 3、super 和 this 不能同时调用构造方法!VS this: 代表的对象不同: this: 本身调用者这个对象 super: 代表父类对象的应用 前提: this: 没有继承也可以使用 super: 只能在继承条件下才能使用 构造方法: this(); 本类的构造 super(); 父类的构造
10、方法重写
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P70
A类
package com.oop.demo07;public class A extends B { //Override 重写 @Override //注解:有功能的注释 public void test() { System.out.println("A=>test()"); }}
B类
package com.oop.demo07;//重写都是方法的重写,和属性无关public class B { public void test(){ System.out.println("B=>test()"); }}
Application 类
package com.oop.demo07;import com.oop.demo06.Student;//测试类public class Application { //静态方法和非静态方法区别很大 public static void main(String[] args) { // 静态方法:方法的调用只和左边,定义的数据有关 // 非静态方法:重写 A a = new A(); a.test(); //A //父类的引用指向了子类 B b = new A(); //子类重写了父类的方法 b.test(); //B }}
note 笔记
重写:需要有继承关系,子类重写父类的方法! 1、方法名必须相同 2、参数列表必须相同 3、修饰符:范围可以扩大,但不能缩小 public > Protected > Default > private 4、抛出的异常:范围可以被缩小,但不能扩大; ClassNotFoundException --> Exception(大)重写:子类的方法和父类必须一致;方法体(内容)不同!为什么需要重写: 1、父类的功能,子类不一定需要,或者不一定满足! Alt + Insert ; override
11、什么是多态
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P71
Person 类
package com.oop.demo08;public class Person { public void run(){ System.out.println("run"); }}/*多态注意事项1; 1、多态是方法的多态,属性没有多态 2、父类和子类,有联系 类型转换异常! ClassCastException(狗不能转成猫) 3、存在的条件: 继承关系,方法需要重写,父类引用指向子类对象! father f1 =new Son(); 不能被重写: 1、static 方法,属于类,它不属于实例 2、final 常量 3、private方法 */
Student 类
package com.oop.demo08;public class Student extends Person{ @Override public void run() { System.out.println("son"); } public void eat(){ System.out.println("eat"); }}
Application 类
package com.oop.demo08;public class Application { public static void main(String[] args) { //一个对象的实际类型是确实的 //new Student(); //new Person(); //可以只指向的引用类型就不确定了: 父类的引用指向子类 //Student 能调到的方法都是自己的或者继承父类的 Student s1 = new Student(); //Person 父类型,可以指向子类,但是不能调用子类独有的方法 Person s2 = new Student(); Object s3 = new Student(); s2.run();//子类重写父类的方法,执行子类的方法 s1.run(); //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大! ((Student)s2).eat(); s1.eat(); }}
12、instanceof 和 类型转换
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P72
Person 类
package com.oop.demo09;public class Person { public void run(){ System.out.println("run"); }}
Student 类
package com.oop.demo09;public class Student extends Person{ public void go(){ System.out.println("go"); }}/* //Object > String //Object > Person > Student //Object > Person > Student Object object = new Student(); //System.out.println(X instanceof Y); //能不能通过编译通过! 有关系才能编译通过,父子 System.out.println(object instanceof Student); //true System.out.println(object instanceof Person); //true System.out.println(object instanceof Object); //true System.out.println(object instanceof Teacher); //false System.out.println(object instanceof String); //false System.out.println("=============="); Person person = new Student(); System.out.println(person instanceof Student); //true System.out.println(person instanceof Person); //true System.out.println(person instanceof Object); //true System.out.println(person instanceof Teacher); //false //System.out.println(person instanceof String); //编译报错 System.out.println("=============="); Student student = new Student(); System.out.println(student instanceof Student); //true System.out.println(student instanceof Person); //true System.out.println(student instanceof Object); //true //System.out.println(student instanceof Teacher); //编译报错 //System.out.println(person instanceof String); //编译报错 */
Student 类
package com.oop.demo09;public class Teacher extends Person{}
Application 类
package com.oop.demo09;import java.time.Year;public class Application { public static void main(String[] args) { //类型之间的转换: 父 子 //高 低 Person obj = new Student(); //obj将这个对象转换为Student类型,我们就可以使用Student类型的方法! ((Student)obj).go(); //子类转换为父类,可能丢失自己本来的一些方法 Student student = new Student(); student.go(); Person person = student; }}/* 1、父类引用指向子类的对象 2、把子类转换为父类,向上转型; 3、把父类转换为子类,向下转型; 强制转换 4、方便方法的调用,减少重复的代码!简洁 封装、继承、多态! 抽象类、接口! */
13、static关键字详解
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P73
Student 类
package com.oop.demo10;//Studentpublic class Student { private static int age; //静态的变量 多线程 private double score; //非静态变量 //非静态方法可以调用静态的方法 public void run(){ go(); } public static void go(){ } public static void main(String[] args) { Student s1 = new Student(); System.out.println(Student.age); //静态的变量,用类名来调用 System.out.println(s1.age); System.out.println(s1.score); go(); //静态方法可以直接调用,它本身就存在在这个类中 }}
Person 类
package com.oop.demo10;public class Person { // 2 { //代码块(匿名代码块) : 赋初始值 System.out.println("匿名代码块"); } // 1 static{ //静态代码块 : 只执行一次 System.out.println("静态代码块"); } // 3 public Person(){ System.out.println("构造方法"); } public static void main(String[] args) { Person person1 = new Person(); System.out.println("============="); Person person2 = new Person(); }}
Test 类
package com.oop.demo10;//静态导入包import static java.lang.Math.random;import static java.lang.Math.PI;public class Test { public static void main(String[] args) { System.out.println(random()); System.out.println(PI); }}
14、抽象类
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P74
Action 类
package com.oop.demo11;
//abstract 抽象类 : 类extends:只能单继承 (接口可以多继承)
public abstract class Action {
//约束 有人帮我们实现~
//abstract 抽象方法,只有方法名字,没有方法的实现!
public abstract void doSomething();
//1、不能new抽象类,只能靠子类去实现它:约束!
//2、抽象类中可以写普通方法
//3、抽象方法必须在抽象类中
//抽象的抽象:约束
//思考题? new 存在构造器吗? 嗯
// 抽象类存在的意义: 提高开发效率
}
A 类
package com.oop.demo11;
//抽象的所有方法,继承了它的子类,都必须实现它的方法, 除非~ 它的子类也是被 abstract 抽象的,就留给子子类解决
public class A extends Action{
@Override
public void doSomething() {
}
}
15、接口的定义与实现
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P75
UseService 类
package com.oop.demo12;
// 抽象的思维 Java
// interface 定义的关键字,接口都需要有实现类
public interface UseService {
//接口中的所有定义其实都是抽象的 public abstract
void add(String name);
void delete(String name);
void update(String name);
void query(String name);
}
TimeService 类
package com.oop.demo12;
public interface TimeService {
void timer();
}
UserServiceImpl 类
package com.oop.demo12;
//抽象类: extends
//类 可以实现接口 implement 接口
//实现了接口的类,就需要重写接口的方法
//多继承~ 利用接口实现多继承
public class UserServiceImpl implements UseService,TimeService {
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void query(String name) {
}
@Override
public void timer() {
}
}
接口的作用
接口的作用:
1、约束
2、定义一些方法。让不同的人实现 10个人 ---> 1个接口
3、public abstract
4、public static final
5、接口不能被实例化,接口中没有构造方法
6、implements可以实现多个接口
7、必须要重写接口中的方法
16、N种内部类
以下内容均来自【狂神说Java】Java零基础学习视频通俗易懂P76
Outer 类
package com.oop.demo13;
public class Outer {
private int id = 10;
public void out(){
System.out.println("这是外部类的方法");
}
public class Inner{
public void in(){
System.out.println("这是内部类的方法");
}
//获得外部类的私有属性
public void getID(){
System.out.println(id);
}
}
//局部内部类
public void method(){
class Inn{
public void in(){
}
}
}
}
//一个java类中可以有多个class类,但只能有一个public class
class A{
public static void main(String[]args){
}
}
Application 类
package com.oop.demo13;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.getID();
}
}
Test 类
package com.oop.demo13;
public class Application {
public static void main(String[] args) {
Outer outer = new Outer();
//通过这个外部类来实例化内部类
Outer.Inner inner = outer.new Inner();
inner.getID();
}
}