java面向对象dome_Java面向对象

本文详细介绍了Java面向对象的三大特性:封装、继承和多态,通过实例演示了值传递与引用传递的区别,并探讨了类与对象、构造器、方法、内存分析以及封装的重要性。此外,还讲解了继承、方法重写、多态的概念,展示了如何利用这些特性实现代码的复用和扩展。
摘要由CSDN通过智能技术生成

面向对象

oop:以类的方式组织代码,以对象的组织(封装)数据

抽象

三大特性:封装 继承 多态;

值传递与引用传递

//值传递

public class Demo03 {

public static void main(String[] args) {

int a = 1;

System.out.println(a);//1

Demo03.change(a);

System.out.println(a);//1

}

public static void change(int a){

a = 10;

}

}

//引用传递:对象,本质还是值传递

public class Demo02 {

public static void main(String[] args) {

Person person = new Person();

System.out.println(person.name);//null

Demo02.change(person);

System.out.println(person.name);//hqw

}

public static void change(Person person){

person.name = "hqw";

}

}

//定义了person类,有一个属性:name

class Person{

String name;

}

类与对象的关系

类与对象

类是一种抽象的数据类型,它是对某一类事物整体描述,但并不能代表某一具体的事物

对象是抽象概念的具体实例

方法

定义、调用

对应的引用

引用类型:基本类型

对象是通过引用来操作的:栈----->堆

属性:字段Field 成员变量

默认初始化:

?数字:0、0.0

?char:u0000

?引用:null

修饰符 属性类型 属性名 = 属性值

对象的创建和使用

必须使用new关键字创建对象,构造器 Person hqw = new Person();

对象的属性 hqw.name

对象的方法:hqw.sleep();

类:

静态的属性 属性

动态的行为 方法

创建初始化对象

使用new关键字创建对象

使用new创建时,除了分配内存空间,还会给创建好的对象进行默认的初始化以及对类中的构造器的调用

类中的构造器也成为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有以下两个特点:

必须和类的名字相同

必须没有返回类型,也不能写void

package Demo02;

public class Person {

//一个构造器什么都不写,也会存在一个方法

//显式的定义构造器

String name;

//实例化初始值

//1.使用new关键字,本质是在调用构造器

//2.用来初始化值

public Person(){

this.name = "qinjinga";

}

//有参构造:一旦定义了有参构造,无参必须显式定义

public Person(String name){

this.name = name;

}

//alt+insert自动创建构造器

}

/*

public class Application{

public static void main(String[] args) {

//new实例化了一个对象

Person person = new Person("hqw");

System.out.println(person.name);

}

}

*/

构造器作用:

new本质在调用构造方法

初始化对象的值

注意点:

定义有参构造之后,如果使用无参构造,显示的定义了一个无参构造

内存分析

package Demo03;

public class Pet {

String name;

int age;

public void shout(){

System.out.println("叫了一声");

}

}

package Demo03;

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);

}

}

infodetail-3729931.html

封装

高内聚,低耦合

//类:private 私有

public class Student {

private String name;//名字

private int id;

private char sex;

private int age;

//提供一些可以操作这个属性的方法

//提供public的 get、set方法

public String getName(){

return this.name;

}

public void setName(String name){

this.name = name;

}

public void setAge(int age){

if (age>120 || age<0){

this.age=3;

}else {

this.age = age;

}

}

//alt + insert 自动生成get、set方法

}

提高程序的安全性,保护数据

隐藏代码的实现细节

统一接口

提高可维护性

继承

继承是对某一批类的抽象,从而实现对现实世界更好的建模

extends 为扩展,是子类对父类的扩展

Java中只有单继承(一个儿子只能有一个爹,但是一个爹可能有多个儿子)

继承关系的两个类,一个为子类(派生类)一个为父类(基类)。子类继承父类,使用关键字extends来表示

子类与父类之间,从意义上讲应该具有“is a”的关系。

package Demo05;

//父类

public class Person {

//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;

}

}

package Demo05;

//子类:继承了父类,就有了父类的全部方法

public class Student extends Person{

//Ctrl + H 调出继承树

}

import Demo05.Student;

public class Application {

public static void main(String[] args) {

Student student = new Student();

student.say();

System.out.println(student.getMoney());

}

}

在java中所有的类都默认直接或间接继承object类

public class Person {

protected String name = "kuangshen";

}

public class Student extends Person{

private String name = "qinjiang";

public void test1(String name){

System.out.println(name);//秦将

System.out.println(this.name);//qinjiang

System.out.println(super.name);//kuangshen

}

}

public class Application {

public static void main(String[] args) {

Student student = new Student();

student.test1("秦将");

}

}

//父类

public class Person {

public void print(){

System.out.println("Person");

}

}

//子类:继承了父类,就有了父类的全部方法

public class Student extends Person{

public void print(){

System.out.println("Student");

}

public void test2(){

print();//Student

this.print();//Student

super.print();//Person

}

}

public class Application {

public static void main(String[] args) {

Student student = new Student();

student.test2();

}

}

//父类

public class Person {

public Person(){

System.out.println("Person无参构造");

}

}

//子类:继承了父类,就有了父类的全部方法

public class Student extends Person{

//隐藏了父类的无参构造调用: super();

//调用父类的构造器,必须要在子类构造器的第一行

public Student(){

System.out.println("Student无参构造");

}

}

public class Application {

public static void main(String[] args) {

Student student = new Student();

//输出:

//Person无参构造

//Student无参构造

}

}

super注意点:

super调用父类的构造方法,必须在构造方法的第一个

super必须只能出现在子类的方法或者构造方法中

super和this不能同时调用构造方法

vs this:

代表的对象不同

this:本身调用者这个对象

super:代表父类对象的引用

前提

this:没有继承也能使用

super:只能在继承条件下使用

构造方法:

this:本类的构造

super:父类的构造

方法重写

重写都是方法的重写,与属性无关

public class A extends B{

public static void test(){

System.out.println("A=>test()");

}

}

public class B {

public static void test() {

System.out.println("B=>test()");

}

}

public class Application {

public static void main(String[] args) {

//方法的调用只和左边定义的数据类型有关

A a = new A();

a.test();

//父类的引用指向了子类

B b = new A();

b.test();

}

}

↑静态方法的调用 结果为:

A=>test()

B=>test()

重写只与非静态方法有关!

public class A extends B{

public void test(){

System.out.println("A=>test()");

}

}

public class B {

public void test() {

System.out.println("B=>test()");

}

}

public class Application {

public static void main(String[] args) {

A a = new A();

a.test();

//父类的引用指向了子类

B b = new A();//子类重写了父类的方法

b.test();

}

}

输出:

A=>test()

A=>test()

重写的关键词只能是public,private也不行

重写:需要有继承关系,子类重写父类的方法!

方法名必须相同

参数列表必须相同

修饰符:范围可以扩大但不能缩小

抛出的异常:范围可以被缩小但不能扩大:ClassNotFoundException --> Exception

重写,子类的方法和父类必须要一致;方法体不同

为什么需要重写:

父类的功能子类不一定需要,或者不一定满足

Alt+Insert : override

多态

public class Person {

public void run(){

System.out.println("run");

}

}

public class Student extends Person {

@Override

public void run(){

System.out.println("son");

}

public void eat(){

System.out.println("ear");

}

}

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();

s1.eat();

s2.eat();//报错,对象能指向哪些方法,主要看左边的类型,和右边关系不大

}

}

注意事项:

多态是方法的多态,属性没有多态

父类和子类有联系 类型转换异常!ClassCastException!

存在条件:继承关系、方法需要重写、父类的引用指向子类对象 Father f1 = new Son()

不能重写的方法:

static 方法:属于类不属于实例

final 常量

private 方法

instanceof与强制转换

instanceof(类型转换)引用类型,判断一个对象是什么类型

public class Application {

public static void main(String[] args) {

//Object > String

//Object > Person > Teacher

//Object > Person > Student

Object object = new Student();

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

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);//编译报错

}

}

强制转换

public class Person {

public void run(){

System.out.println("run");

}

}

public class Student extends Person {

public void go(){

System.out.println("go");

}

}

public class Application {

public static void main(String[] args) {

//类型间的转换:父(高) 子(低)

//低转高不需要强制类型转化,高转低需要

//高 低

Person obj = new Student();

//将这个对象转换为Student类型就可以使用student类型的方法了

Student student = (Student) obj;

student.go();

//简写为((Student)obj).go

//子类转换为父类,可能丢失自己本身的方法

Student student1 = new Student();

student.go();

Person person = student1;//低转高自动转

person.go()//报错

}

}

父类的引用指向子类的对象(不可逆)

把子类转换为父类,向上转型:可能会丢失方法

把父类转换为子类,向下转型:强制转换

方便方法调用,减少重复的代码

static关键字

public 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(age);//age.sout

System.out.println(s1.age);

System.out.println(score);//报错

System.out.println(s1.score);

go();//调用静态方法

new Student().run();//非静态用对象调用

}

}

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 person = new Person();

Person person2 = new Person();//第二次执行没有静态代码块

}

}

执行顺序↑

//静态导入包

import static java.lang.Math.random;

public class Test {

public static void main(String[] args) {

System.out.println(random());//写了导入包就不用写Math.random()

}

}

final类不能被继承

抽象类

abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法;如果修饰类,那么就是该类的抽象类.

不能new抽象类,只能靠子类去实现它;约束!

抽象方法必须在抽象类中

抽象类可以有普通方法

抽象方法只有方法的声明,没有方法的实现,它是用子类实现的

子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该子类也设为抽象类

package Demo09;

//抽象类 extends:单继承 接口可以多继承

public abstract class Action {

//约束 有人帮我们实现

//只有抽象的约束没有实现

public abstract void doSomething();

}

package Demo09;

//抽象类的所有方法,继承了它的子类,都必须要实现它的方法,除非本身也是抽象类

public class A extends Action{

@Override

public void doSomething() {

}

}

接口

interface

普通类:只有具体实现

抽象类:具体实现和规范(抽象方法)都有!

接口: 只有规范!自己无法写方法

接口的本质是契约

package Demo10;

//抽象的思维是关键

//interface 定义的关键字,接口都需要有实现类

public interface UserService {

//常量 public static final

int AGE = 99;

//接口里的所有定义都是抽象的public abstract

void run(String name);

void delete(String name);

void update(String name);

void query(String name);

}

public interface TimeService {

void timer();

}

//类可以实现接口 implements

//实现了接口的类,就需要重写接口中的方法

//用接口实现多继承

public class UserServiceImpl implements UserService, TimeService{

@Override

public void run(String name) {

}

@Override

public void delete(String name) {

}

@Override

public void update(String name) {

}

@Override

public void query(String name) {

}

@Override

public void timer() {

}

}

作用:

约束

定义一些方法, 让不同的人实现

方法都是: public abstract

常量都是: public static final

接口不能被实例化, 接口中没有构造方法

implements可以实现多个接口

必须要重写接口中的方法

内部类

在一个类的内部再定义一个类

一个java类中可以有多个class类, 但是只能有一个public class

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 class Application{

public static void main(String[] args) {

Outer outer = new Outer();

//通过外部类来实例化内部类

Outer.Inner inner = outer.new Inner();

inner.in();

inner.getID();

}

}

package Demo11;

public class Test {

public static void main(String[] args) {

//匿名内部类: 没有名字初始化类,不用将实例保存到变量中

new Apple().eat();

UserService userService = new UserService() {

@Override

public void hello(){

}

};

}

}

class Apple{

public void eat(){

System.out.println("eat");

}

}

interface UserService{

void hello();

}

原文:https://www.cnblogs.com/squirrelV/p/14443210.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值