注:学习笔记
一、什么是面向对象
属性+方法 = 类
面向过程:所要做什么事情,步骤明了,一步一步来;处理一些简单问题
面向过程 例如张三打篮球
面向对象,人的对象,人的运动动作,运动的器械这三个对象
实例化一个张三的对象,对象有一个打篮球的动作,器械是篮球
实例化一个张四的对象,对象有一个打篮球的动作,器械是篮球
面向对象能够更好的在抽象的层面来分析问题,在实现程序跨越极大赋予之前代码
面向过程编程很难实现
面向对象(oop)本质:以类的方式组织代码,以对象的组织(封装)数据;
核心思想:抽象;
三大特性:封装,继承,多态(同一个事物,他会有多种形态)
二、简单方法
静态方法和非静态方法:
public class Demo02 {
//static和类一起加载的
public static void a(){
b();
}
//类实例化后才存在
public void b(){
a();
}
}
形参和实参:
public static void main(String[] args) {
//实际参数类型和形式参数类型要一一对应
int add = Demo03.add(1, 2);//实际参数 1, 2
System.out.println(add);
}
public static int add(int a,int b){//形式参数 int a,int b
return a+b;
}
值传递与引用传递:
//值传递
public static void main(String[] args) {
int a = 1;
System.out.println(a);//1
chage(a);
System.out.println(a);//1
}
public static void chage(int a){
a = 10;
}
//引用传递 本质还是值传递
public class Demo04 {
public static void main(String[] args) {
Perosn perosn = new Perosn();
System.out.println(perosn.name);//空
change(perosn);
System.out.println(perosn.name);//
}
public static void change(Perosn perosn){
//perosn是一个对象 指向----->Perosn perosn = new Perosn();这是一个具体的人,可以改变属性
perosn.name = "哈哈哈";
}
}
//定义了一个Perosn类,有一个属性:name
class Perosn{
String name; //null
}
三、类与属性
一个类里面只有属性和方法
public class Student {
String name;
int age;
//方法
public void study(){
System.out.println(this.name+"...");
}
}
public class Application {
public static void main(String[] args) {
//类:抽象的,要对他进行实例化
Student mzq = new Student();
Student xh = new Student();
mzq.name = "孟志强";
mzq.age = 10;
System.out.println(mzq.name);
System.out.println(mzq.age);
System.out.println(xh.name);
System.out.println(xh.age);
}
}
四、构造器
public class Persion {
//一个类,即使什么也不写,也会默认存在一个函数
String name;
//1.使用new关键字,必须要有构造器,本质是调用构造器
//2.构造器一般用来初始化值
public Persion(){
this.name = "小明";
}
//有参构造:一当定义了有参构造,无参就必须显示定义
public Persion(String name){
this.name = name;
}
}
/*
public class Application {
public static void main(String[] args) {
//类:抽象的,要对他进行实例化
Persion persion = new Persion();
System.out.println(persion.name);
}
}
构造器:
1.和类名相同
2.没有返回值
作用:
1.new 实例化 调用构造方法
2.初始化对象的值
注:
定义有参构造之后,如果使用无参构造,要显示定义一个无参的构造
ait+Insert
this.name this.表示当前类 name表示值
*/
五、封装
简单理解:写了一个方法,直接调用这个方法,不用去管这个方法是怎么写的;属性私有,get/set
封装意义:
- 提高程序安全性,保护数据;
- 隐藏代码实现细节;
- 统一接口;
- 系统可维护性;
public class Appliction {
public static void main(String[] args) {
Student s1 = new Student();
//String name = s1.getName();//通过get去获得name的属性
s1.setName("小明");
System.out.println(s1.getName());
}
}
//类
public class Student {
//属性私有
//姓名
private String name; //private->私有 和publi对应
//学号
private int id;
//性别
private char sex;
//提供可以操作这些属性的方法
//提供一些public的get,set方法
//get获得数据
public String getName(){
return this.name;
}
//set给这个数据设置值
public void setName(String name){ //必须要有一个参数
this.name = name;
}
//alt+ insert 生成get,set
}
六、继承
- 继承的本质是对类的抽象,从而实现更好的建模
- 继承是类和类之间的一种关系;继承关系俩个类,一个为子类(派生类)一个为父类,使用关键字extends来表示。子类和父类之间,从意义上讲应该具有“is a”的关系;
- Java中只有单继承,没有多继承;
import com.base.opp.demo05.Student;
public class Appliction {
public static void main(String[] args) {
Student student = new Student();
student.say();//讲了一句好
System.out.println(student.money); //100000
}
}
//在Java中,所有的类都是直接或间接继承object类
//Person 人
public class Person {
public int money = 100000;
public void say(){
System.out.println("讲了一句好");
}
}
//学生 is 人
public class Student extends Person{ //学生继承了人的所有公有属性,私有无法继承
//extends 意思是扩展,子类是父类的扩展
//ctrl+ H 可查看继承
}
//Teacher is Person
public class Teacher extends Person {
}
七、super
super 和this对比
super表示父
this表示当前
super注意点:
1.supeu调用父类的构造方法,必须在构造方法的第一个;
2.super必须只能出现在子类的方法或者构造方法中;
3.super和this不能同时调用构造方法!
vs this:
代表对象不同:
this:本身的调用
super:父类对象的调用
前提:
this:没有继承也可以调用;
super:只能在继承条件下才使用
构造方法:
this():本类的构造;
super():父类的构造
八、方法重写
静态方法
自动生成重写
有这个符号才是重写
public class Appliction {
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
* Protected可以扩大为public 当然private是不能用在这里
* 4.抛出的异常:可以被缩小但是不可能被扩大
*
*重写,子类的方法和父类方法必须一致,方法体不同;
*
* 为什么要重写:
* 1.父类的功能,子类不需要,或者不一定满足;
* 2.
* */
public class A extends B{
//Override 重写
@Override //注解:有功能的注释
public void test() {
//super.test();
System.out.println();
}
}
public class B {
public void test(){
System.out.println("B->test");
}
}
九、继承
public class Appliction {
public static void main(String[] args) {
//一个对象的实际类型是确定的
//new Student();
//new Person();
//但可以指向引用类型就不确定
Student s1 = new Student();
//Student能调用的方法都是自己或继承的父类
Person s2 = new Student(); //父类的引用指向子类
//Person 父类 可以指向子类,但是不能调用子类独有的方法
Object s3 = new Student();
//对象能执行哪些方法看对象左边的类型和右边关系不大
s2.run(); // run 依旧走了父类的方法
//son 子类重写了父类的方法
s1.run();//son
}
/*
多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类有联系 类型转换异常:ClassCastException!
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象
不能重写方法:
1.static方法,属于类,不属于实例化
2.final 常量
3.private 私有的方法
*/
}
public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
}
十、instanceof和类型转换
instanceof:可以判断父子之间是否有关系,判断一个对象是什么类型
public class Appliction {
public static void main(String[] args) {
//类型之间转换:父(高) 子(低)
//高 低
Person person = new Student();//低转高不需要强制转化
//person将这个对象转换为Student类型,我们就可以使用Student类型的方法了!
Student s1=(Student)person; //高转低
s1.go();
//((Student)person).go();
//子类转父类可能会丢失一些自己的方法
}
}
/*
* Object object = new Student();
//Object>Person>Student
//Object>Person>Teacher
//Object>String
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);//编译报错 person String是同级
Student student = new Student();
System.out.println("====================");
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(student instanceof String); //报错
* */
/*
1.父类引用指向子类的对象
2.把子类转换成父类,向上转型
3.把父类转换成子类,向下转型,强制转换(高转低)
4.方便方法调用,减少代码重复
抽象:封装 继承 多态
*/
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
public class Person {
public void run(){
System.out.println("run");
}
}
public class Teacher extends Person {
}
十一、static
public class Student {
private static int age; //静态变量
private double score; //非静态变量
public void sun(){
}
public static void go(){
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.age);
System.out.println(Student.age);
System.out.println(s1.score);
//System.out.println(Student.score);//报错 score是非静态
//run();//无法直接调用
new Student().sun(); //这样才能调用run
go();//可以直接调用,因为他是静态的static
}
}
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();
System.out.println("==========");
Person person2 = new Person();//static没有执行,由此静态方法只执行一次
}
}
//静态导入包
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
//没有导入包
System.out.println(Math.random()); //Math.random()随机生成一个数
//导入静态包
System.out.println(random());
}
}
十二、抽象类
//abstract 抽象类
public abstract class Action {
//abstract 抽象方法,只有方法名,没有方法的实现
public abstract void sun();
//1.不能new抽象类,只能靠子类去实现它:约束!
//2.抽象类中能写普通方法
//3.抽象方法必须在抽象类中
}
//抽象类方法,继承了他的类就必须重写方法,
/*
public class A extends Action{
@Override
public void sun() {
}
}
*/
//除非这样
public abstract class A extends Action{
}
十三、接口
规范化,自己无法写方法
作用:
1.约束
2.定义一些方法,让不同的人实现
3.方法都是public abstract
4.变量都是public static final
5.接口不能被实例化,接口中没有构造方法
6.可以实现多个接口,但必须重写接口里面的方法
//接口必须要有实现类UserServiceImpl
//定义关键字 interface
public interface UserService {
//接口中所有的定义是抽象的 public abstract
void add(String name);
void delete(String name);
void update(String name);
void quer(String name);
}
public interface TimeService {
void timer();
}
//抽象类extends 是单继承,但implements接口可以继承多个
//类可以实现接口,通过implements
//TimeService从侧面实现多继承
public class UserServiceImpl implements UserService ,TimeService {
//实现了接口中的类,就必须重写接口中方法
@Override
public void add(String name) {
}
@Override
public void delete(String name) {
}
@Override
public void update(String name) {
}
@Override
public void quer(String name) {
}
@Override
public void timer() {
}
}
十四、内部类
内部类就是在一个类的内部在定义一个类,比如,A类中定义一个B类,那么B类就称为内部类,A类相对于B类来说就是外部类。
public class Outer {
private int id=123;
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();
}
}