面向对象
什么是面向对象
方法的定义
package com. liu. oop ;
import java. io. IOException ;
public class Demo01 {
public static void main ( String [ ] args) {
}
public String sayHello ( ) {
return "Hello,world!" ;
}
public void hello ( ) {
return ;
}
public int max ( int a, int b) {
return a> b ? a: b;
}
public void raedFile ( String file) throws IOException {
}
}
方法的调用
静态方法/实参和形参
package com. liu. oop ;
public class Demo03 {
public static void main ( String [ ] args) {
int add = Demo03 . add ( 1 , 2 ) ;
System . out. println ( add) ;
}
public static int add ( int a, int b) {
return a+ b;
}
}
非静态方法/实参和形参
package com. liu. oop ;
public class Demo03_1 {
public static void main ( String [ ] args) {
int Demo03_1 = new Demo03_1 ( ) . add ( 1 , 2 ) ;
System . out. println ( Demo03_1 ) ;
}
public int add ( int a, int b) {
return a+ b;
}
}
值传递
package com. liu. oop ;
public class Demo04 {
public static void main ( String [ ] args) {
int a= 1 ;
System . out. println ( a) ;
Demo04 . change ( a) ;
System . out. println ( a) ;
}
public static void change ( int a) {
a= 10 ;
}
}
引用传递
package com. liu. oop ;
public class Demo05 {
public static void main ( String [ ] args) {
Person person = new Person ( ) ;
System . out. println ( person. name) ;
Demo05 . change ( person) ;
System . out. println ( person. name) ;
}
public static void change ( Person person) {
person. name= "louis" ;
}
}
class Person {
String name;
}
类与对象
类与对象的创建
类与对象的关系
创建与初始化对象
学生类
package com. liu. oop. demo02 ;
public class Student {
String name;
int age;
public void study ( ) {
System . out. println ( this . name+ "在学习" ) ;
}
}
主类(一个项目只能有一个main方法)
package com. liu. oop. demo02 ;
import com. liu. oop. demo02. Student ;
public class Application {
public static void main ( String [ ] args) {
Student xiaoming = new Student ( ) ;
Student xiaohong = new Student ( ) ;
xiaoming. name = "小明" ;
xiaoming. age = 18 ;
System . out. println ( xiaoming. name) ;
System . out. println ( xiaoming. age) ;
System . out. println ( xiaohong. name) ;
System . out. println ( xiaohong. age) ;
}
}
构造器详解
主类
package com. liu. oop. demo03 ;
import com. liu. oop. demo03. Person ;
public class Application {
public static void main ( String [ ] args) {
Person personwc = new Person ( ) ;
Person personyc = new Person ( "Louis" ) ;
System . out. println ( personwc. name) ;
System . out. println ( personyc. name) ;
}
}
Person类
package com. liu. oop. demo03 ;
public class Person {
String name;
public Person ( ) {
this . name = "Louis" ;
}
public Person ( String name) {
this . name = name;
}
}
创建对象内存分析
Pet类
package com. liu. oop. demo04 ;
public class Pet {
public String name;
public int age;
public void shout ( ) {
System . out. println ( name+ "叫了一声" ) ;
}
}
主类
package com. liu. oop. demo04 ;
import com. liu. oop. demo04. 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. age) ;
System . out. println ( dog. name) ;
System . out. println ( "===============" ) ;
Pet cat = new Pet ( ) ;
dog. name = "小白" ;
dog. age = 3 ;
dog. shout ( ) ;
System . out. println ( dog. age) ;
System . out. println ( dog. name) ;
}
}
简单小结
1. 类与对象
类是一个模板:抽象,对象是一个具体的实例
2. 方法
定义、调用!
3. 对应的引用
引用类型:基本类型(8 )
对象是通过引用来操作的:栈== == 》堆
4. 属性:字段field 成员变量
默认初始化:
数字:0 0.0
char :u0000
boolean :false
引用:null
修饰符 属性类型 属性名 = 属性值
5. 方法
6. 对象的创建和使用
- 必须使用new 关键字创建对象, 构造器 Person Louis = new Person ( ) ;
- 对象的属性 Louis . name
- 对象的方法 Louis . sleep ( )
7. 类
静态的属性 属性
动态的行为 方法
封装、继承、多态
封装
Student类
package com. liu. oop. demo05 ;
public class Student {
private String name;
private int id;
private char sex;
private int age;
public String getName ( ) {
return this . name;
}
public void setName ( String name) {
this . name = name;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public char getSex ( ) {
return sex;
}
public void setSex ( char sex) {
this . sex = sex;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
if ( age> 120 || age< 0 ) {
this . age = 10086 ;
} else {
this . age = age;
}
}
}
主类
package com. liu. oop. demo05 ;
import static sun. security. pkcs11. wrapper. Functions . getId ;
public class Application {
public static void main ( String [ ] args) {
Student s1 = new Student ( ) ;
Student s2 = new Student ( ) ;
s1. setName ( "louis" ) ;
s1. setId ( 1 ) ;
s1. setSex ( '男' ) ;
s1. setAge ( 18 ) ;
s2. setAge ( 79789 ) ;
System . out. println ( s1. getName ( ) ) ;
System . out. println ( s1. getId ( ) ) ;
System . out. println ( s1. getSex ( ) ) ;
System . out. println ( "s1age:" + s1. getAge ( ) ) ;
System . out. println ( "s2age:" + s2. getAge ( ) ) ;
}
}
继承
Java中类只有单继承,没有多继承!
一个子类只能有一个父类,但是一个父类可以有多个子类
继承基础认识
主类
package com. liu. oop. demo6 ;
public class Application {
public static void main ( String [ ] args) {
Student student = new Student ( ) ;
student. say ( ) ;
System . out. println ( student. money) ;
}
}
Person类
package com. liu. oop. demo6 ;
public class Person {
public void say ( ) {
System . out. println ( "说了一句话" ) ;
}
public int money = 10_0000_0000 ;
}
学生类
package com. liu. oop. demo6 ;
public class Student extends Person {
}
老师类
package com. liu. oop. demo6 ;
public class Teacher extends Person {
}
super - this
super注意点:
1 .super调用父类的构造方法,必须在构造方法的第一个
2 .super必须只能在子类的方法或者构造方法中!
3 .super和this不能同时调用构造方法!
VS this:
代表的对象不同:
this:本身调用者这个对象
super: 代表父类对象的应用
前提
this: 没有继承也可以使用
super: 只能在继承条件才可以使用
构造方法
this( ) :本类的构造
super( ) :父类的构造
主类
package com. liu. oop. demo07 ;
import com. liu. oop. demo07. Student ;
import com. liu. oop. demo07. Person ;
public class Application {
public static void main ( String [ ] args) {
Student student = new Student ( ) ;
student. test1 ( "清风" ) ;
student. test2 ( ) ;
}
}
Person类
package com. liu. oop. demo07 ;
public class Person {
public Person ( ) {
System . out. println ( "Person无参执行了" ) ;
}
protected String name = "louis" ;
public void print ( ) {
System . out. println ( "Person" ) ;
}
}
Student主类
package com. liu. oop. demo07 ;
import com. liu. oop. demo07. Person ;
public class Student extends Person {
public Student ( ) {
super ( ) ;
System . out. println ( "Student无参执行了" ) ;
}
private String name = "qf" ;
public void print ( ) {
System . out. println ( "Student" ) ;
}
public void test1 ( String name) {
System . out. println ( name) ;
System . out. println ( this . name) ;
System . out. println ( super . name) ;
}
public void test2 ( ) {
print ( ) ;
this . print ( ) ;
super . print ( ) ;
}
}
方法重写
static关键字(拓展)
在Java中,方法是否加上static关键字有着明显的区别,主要体现在以下几个方面:
1 .访问方式:
静态方法(加static)可以直接通过类名来访问,不需要创建类的实例。
非静态方法(不加static)需要先创建类的一个实例,然后通过这个实例来访问。
2 .可访问的变量和方法:
静态方法只能访问静态变量和静态方法,因为它不依赖于任何特定的对象实例。
非静态方法可以访问实例变量和实例方法,也可以访问静态变量和静态方法。
3 .内存中的存在形式:
静态方法随着类的加载而加载到内存中,并且在整个应用程序运行期间都存在于内存中,直到JVM关闭。
非静态方法随着对象的创建而加载到内存中,并且当对象被垃圾回收时,这些方法也会被销毁。
4 .多线程中的锁定行为:
如果一个方法没有加static,那么同步操作(如synchronized)锁住的是调用该方法的对象实例。
如果一个方法加了static,那么同步操作锁住的是类的Class对象,即对于所有的实例来说都是同一个锁。
5 .使用this和super:
静态方法中不能使用this或super关键字,因为它们是针对具体实例的引用。
非静态方法可以使用this和super关键字来引用当前对象实例和父类。
6 .初始化时机:
类的静态部分(包括静态方法和静态变量)会在类首次被加载时初始化。
类的非静态部分(包括非静态方法和非静态变量)会在创建类的第一个实例时初始化。
总结来说,static关键字决定了方法是否属于类本身而不是类的实例。选择使用静态方法还是非静态方法取决于具体的应用场景和设计需求。
重载出现的指示(原为静态方法,重载的是非静态方法)
方法重写(alt +inster 笔记本:Fn+alt+inster ===>Override)
A类
package com. liu. oop. demo08 ;
public class A extends B {
@Override
public void test ( ) {
System . out. println ( "A===>test" ) ;
}
}
B类
package com. liu. oop. demo08 ;
public class B {
public void test ( ) {
System . out. println ( "B===>test" ) ;
}
}
主类
package com. liu. oop. demo08 ;
public class Application {
public static void main ( String [ ] args) {
A a = new A ( ) ;
a. test ( ) ;
B b = new A ( ) ;
b. test ( ) ;
}
}
方法重写小结
重写:需要有继承关系,子类重写父类的方法!
1 .方法名必须相同
2 .参数列表列表必须相同
3 .修饰符:范围可以扩大但不能缩小:public> protected> default> private
4 .抛出的异常:范围可以被缩小但不能扩大:EXception(大)>> ClassNotfoundException
重写,子类的方法和父类必要一致,方法体不同!
为什么需要重写:
1 .父类的功能,子类不一定需要,或者不一定满足!
Alt+Insert: Override
多态
Person类
package com. liu. oop. demo09 ;
public class Person {
public void run ( ) {
System . out. println ( "run" ) ;
}
}
Student类
package com. liu. oop. demo09 ;
public class Student extends Person {
@Override
public void run ( ) {
System . out. println ( "son,run" ) ;
}
public void eat ( ) {
System . out. println ( "eat" ) ;
}
}
主类
package com. liu. oop. demo09 ;
public class Application {
public static void main ( String [ ] args) {
Student s1 = new Student ( ) ;
Person s2 = new Student ( ) ;
Object s3 = new Student ( ) ;
s2. run ( ) ;
s1. run ( ) ;
( ( Student ) s2) . eat ( ) ;
}
}
多态注意事项
多态注意事项:
1 . 多态是方法的多态,属性没有多态
2 . 父类和子类,有联系 类型转换异常! :ClassCastException!
3 . 存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son( ) ;
不能重写方法:
1 . static 方法,属于类,它不属于实例
2 . final 常量
3 . private 方法
instanceof关键字用于检查:
·一个对象是否属于特定类或其子类。
·返回值为布尔类型,如果是则返回true,否则返回false。
主类
package com. liu. oop. demo10 ;
public class Application1 {
public static void main ( String [ ] args) {
Object object = new Student ( ) ;
System . out. println ( object instanceof Student ) ;
System . out. println ( object instanceof Person ) ;
System . out. println ( object instanceof Object ) ;
System . out. println ( object instanceof Teacher ) ;
System . out. println ( object instanceof String ) ;
System . out. println ( "====================================" ) ;
Person person = new Student ( ) ;
System . out. println ( person instanceof Student ) ;
System . out. println ( person instanceof Person ) ;
System . out. println ( person instanceof Object ) ;
System . out. println ( person instanceof Teacher ) ;
System . out. println ( "====================================" ) ;
Student student = new Student ( ) ;
System . out. println ( student instanceof Student ) ;
System . out. println ( student instanceof Person ) ;
System . out. println ( student instanceof Object ) ;
}
}
Person类
package com. liu. oop. demo10 ;
public class Person {
public void run ( ) {
System . out. println ( "run" ) ;
}
}
Student类
package com. liu. oop. demo10 ;
import com. liu. oop. demo10. Person ;
public class Student extends Person {
public void go ( ) {
System . out. println ( "go" ) ;
}
}
Teacher类
package com. liu. oop. demo10 ;
public class Teacher extends Person {
}
类型转换(其他类同instanceof内容)
1 .父类引用指向子类的对象
2 .把子类转换为父类,向上转型
3 .把父类转换为子类,向下转型 (强制转型)
4 .方便方法的调用,减少重复的代码! 简洁
package com. liu. oop. demo10 ;
public class Application2 {
public static void main ( String [ ] args) {
Person student = new Student ( ) ;
Student s = ( Student ) student;
s. go ( ) ;
( ( Student ) student) . go ( ) ;
Student student1 = new Student ( ) ;
student1. go ( ) ;
Person person = student1;
}
}
static关键字详解
主类
package com. liu. oop. demo11 ;
public class Application {
}
Person类
package com. liu. oop. demo11 ;
public final class Person {
{
System . out. println ( "Person的匿名代码块" ) ;
}
static {
System . out. println ( "Person的静态代码块" ) ;
}
public Person ( ) {
System . out. println ( "Person的构造方法" ) ;
}
public static void main ( String [ ] args) {
Person p1 = new Person ( ) ;
System . out. println ( "=======================" ) ;
Person p2 = new Person ( ) ;
}
}
Student类
package com. liu. oop. demo11 ;
public class Student {
private static int age;
private double score;
public static void go ( ) { }
public void run ( ) {
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) ;
s1. run ( ) ;
go ( ) ;
}
}
Test类
package com. liu. oop. demo11 ;
import static java. lang. Math . random ;
import static java. lang. Math . PI ;
public class Test {
public static void main ( String [ ] args) {
System . out. println ( Math . random ( ) ) ;
System . out. println ( random ( ) ) ;
System . out. println ( PI ) ;
}
}
抽象类
主类
package com. liu. oop. demo12 ;
public class Application {
}
A类
package com. liu. oop. demo12 ;
public class A extends Action {
@Override
public void doSomething ( ) {
}
}
Action类
package com. liu. oop. demo12 ;
public abstract class Action {
public abstract void doSomething ( ) ;
}
接口
接口的作用
作用:
1 .约束
2 、定义一些方法,让不同的人实现
3 .方法:public abstract
4 .常量:public static final
5 .接口不能被实例化~,接口中没有构造方法~
6 .implements可以实现多个接口
7 .必须重写接口里面的方法
UserServices接口
package com. liu. oop. demo13 ;
public interface UserServices {
public abstract void run ( ) ;
void eat ( ) ;
void sleep ( String something) ;
int AGE = 99 ;
public static final int DAY = 99 ;
}
TimeServices接口
package com. liu. oop. demo13 ;
public interface TimeServices {
void timer ( ) ;
}
UserServicesImpl类
package com. liu. oop. demo13 ;
public class UserServicesImpl implements UserServices , TimeServices {
@Override
public void eat ( ) {
System . out. println ( "吃东西" ) ;
}
@Override
public void run ( ) {
System . out. println ( "跑" ) ;
}
@Override
public void sleep ( String something) {
System . out. println ( "睡" + something) ;
}
@Override
public void timer ( ) {
}
}
内部类
Outer类
package com. liu. oop. demo14 ;
public class Outer {
private int id= 10 ;
public void out ( ) {
System . out. println ( "这是外部类的方法" ) ;
}
public void method ( ) {
class Inner {
public void in ( ) {
System . out. println ( "这是局部内部类的方法" ) ;
}
}
}
public class Inner {
public void in ( ) {
System . out. println ( "这是成员内部类的方法" ) ;
}
public void getID ( ) {
System . out. println ( id) ;
}
}
public static class StaticInner {
public void in ( ) {
System . out. println ( "这是静态内部类的方法" ) ;
}
}
}
class A {
void show ( ) {
System . out. println ( "也是一个内部类" ) ;
}
}
interface B {
public void hello ( ) ;
}
主类
package com. liu. oop. demo14 ;
public class Application {
public static void main ( String [ ] args) {
Outer outer = new Outer ( ) ;
Outer. Inner inner = outer. new Inner ( ) ;
inner. getID ( ) ;
new A ( ) . show ( ) ;
new B ( ) {
@Override
public void hello ( ) {
System . out. println ( "这是匿名内部类的方法" ) ;
}
} ;
}
}