面向过程思想—步骤清晰简单,第一步做什么,第二步做什么...
适合处理一些较为简单的问题
面向对象思想—分类的思维模式,思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独思考
适合处理一些较为复杂的问题
对于描述复杂的事物,要从整体上进行合理分析,需要通过面向对象的思路来分析整个系统,然后通过面向过程的思路去处理具体的微观操作
面向对象编程(OOP)—以类的方式组织代码,以对象的方式组织(封装)数据
类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但并不能代表某一个具体的事物
对象是抽象概念的具体事例
①创建一个类
1 public class Student {
2 //属性
3 String name;
4 int age;
5 ?
6 //方法
7 public void study(){
8 System.out.println(name+"在学习");
9 }
10 }
②在应用类调用该类
1 public class Application {
2 //一个项目里只有一个主函数
3 public static void main(String[] args) {
4 //类:抽象的、实例化
5 //类实例化后返回一个自己的对象
6 Student stu=new Student(); //stu是Student类的具体实例
7 stu.name="sumAll";
8 stu.study();
9 }
10 }
①类中的构造器也称为构造方法,是在创建对象的时候必须要调用的
②构造器特点
1.必须和类的名字相同
2.必须没有返回类型,也不能写void
3.默认存在无参数的构造方法
4.使用new关键字,本质是在调用构造器
5.一旦定义了有参构造方法,无参构造方法必须显式定义
6.构造方法可以重载,在new时会根据参数列表自动匹配相应的构造方法
1 public class Student {
2 //属性
3 String name;
4 int age;
5 ?
6 //方法
7 public void study(){
8 System.out.println(name+"在学习");
9 }
10 //有参构造方法
11 public Student(String name, int age) {
12 this.name = name;
13 this.age = age;
14 }
15 //无参构造方法
16 public Student(){}
17 }
有如下代码
1 //Pet类
2 public class Pet {
3 String name;
4 int age;
5 public void shout(){
6 System.out.println("叫一声");
7 }
8 }
9 //应用类
10 public class Application {
11 public static void main(String[] args) {
12 Pet dog=new Pet();
13 dog.name="汪";
14 dog.age=3;
15 dog.shout();
16 }
17 }
①先执行main()方法,把main()方法保存进方法区,然后在栈中入栈一个main()引用变量名,如下所示
②执行Pet类的实例化语句,在方法区中保存Pet类的方法,在栈中入栈相应的引用变量名,在堆中保存相关的属性,如下所示
③为dog对象赋值,修改堆中属性值;调用shout()方法,调用方法区中的shout()方法,如下所示
①封装—数据的隐藏,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,称为信息隐蔽
②封装的目的是为了追求“高内聚,低耦合”
③属性要私有,通过set()方法来修改,通过get()方法来获取
④封装的意义
1.提高程序的安全性,保护数据
2.隐藏代码的实现细节
3.可统一接口,形成一定规范
4.提高系统的可维护性
1 public class Person {
2 private String name;
3 private int age;
4 ?
5 public String getName() {
6 return name;
7 }
8 ?
9 public int getAge() {
10 return age;
11 }
12 ?
13 public void setName(String name) {
14 this.name = name;
15 }
16 ?
17 public void setAge(int age) {
18 this.age = age;
19 }
20 }
①继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模,通过关键字extends来实现
②Java中类只有单继承,没有多继承
③继承是类和类之间的关系;继承关系的两个类,一个为子类(派生类),一个为父类(基类)
④在Java中,所有的类默认直接或间接继承Object
⑤子类可以使用从父类的继承过来的方法
1 //Person类 父类
2 public class Person {
3 private String name;
4 ?
5 public String getName() {
6 return name;
7 }
8 public void setName(String name) {
9 this.name = name;
10 }
11 public String say(){
12 return "Hello";
13 }
14 }
15 ?
16 //Student is Person,子类
17 public class Student extends Person{
18 private float score;
19 ?
20 public float getScore() {
21 return score;
22 }
23 ?
24 public void setScore(float score) {
25 this.score = score;
26 }
27 }
28 ?
29 //Teacher is Person,子类
30 public class Teacher extends Person{
31 private float salary;
32 ?
33 public float getSalary() {
34 return salary;
35 }
36 ?
37 public void setSalary(float salary) {
38 this.salary = salary;
39 }
40 }
41 ?
42 //应用类
43 public class Application {
44 public static void main(String[] args) {
45 Student student=new Student();
46 Teacher teacher=new Teacher();
47 student.setName("A");student.setScore(100);
48 teacher.setName("B");teacher.setSalary(100000);
49 System.out.println(student.getName()+"说"+student.say()+",成绩是"+student.getScore());
50 System.out.println(teacher.getName()+"说"+teacher.say()+",成绩是"+teacher.getSalary());
51 }
52 }
53 ?
54 /*
55 输出结果是:
56 A说Hello,成绩是100.0
57 B说Hello,成绩是100000.0
58 */
①子类实例化的时候,会调用父类构造器,也就是在子类构造器中隐含super();语句,并且该语句必须是第一行
②父类中只有有参构造方法,则子类中的构造方法中必须显式的调用父类的有参构造方法,如super("name");
③注意点:
1.super调用父类的构造方法,必须在构造方法的第一个
2.super必须只能出现在子类的方法或者构造方法中
3.super和this不能同时调用构造方法
④super和this的区别
1.代表的对象不同—this代表本身调用者这个对象;super代表父类对象
2.前提不同—this没有继承也可以使用;super只能在继承条件下才可以使用
3.构造方法不同—this()代表本类的构造方法;super()代表父类的构造方法
1 //父类
2 public class Persons {
3 private String name;
4 protected int age;
5 public Persons(String name){
6 this.name=name;
7 System.out.println("Person构造");
8 }
9 ?
10 public void say(){
11 System.out.println("Person说");
12 }
13 }
14 ?
15 //子类
16 public class Students extends Persons {
17 private float score;
18 public Students(String name){
19 super(name);//调用父类的有参构造方法
20 System.out.println("Student构造");
21 }
22 public void say(){
23 super.say();//调用父类的say()方法
24 super.age=3;//修改父类的age属性
25 System.out.println("Student说");
26 }
27 }
28 ?
29 //应用类
30 public class Application {
31 public static void main(String[] args) {
32 Students student=new Students("A");
33 student.say();
34 }
35 }
36 ?
37 /*
38 输出结果是:
39 Person构造
40 Student构造
41 Person说
42 Student说
43 */
①重写需要有继承关系,子类重写父类的方法
②特点:
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大,但不能缩小—public→protected→default→private
4.抛出的异常:范围可以被缩小,但不能扩大—ClassNotFoundException→Exception
③重写具有的意义—父类的功能子类不一定需要,或者不一定满足
快捷方式是Alt+Insert,再点击override
④不能重写的方法
1.static修饰的方法—属于类,不属于实例
2.final修饰的方法—在常量池中
3.private修饰的方法—私有,无法被继承
1 //父类
2 public class A {
3 public void test(){
4 System.out.println("A→test()");
5 }
6 }
7 ?
8 //子类
9 public class B extends A{
10 @Override
11 public void test() {
12 System.out.println("B→test()");
13 }
14 }
15 ?
16 //应用类
17 public class Application {
18 public static void main(String[] args) {
19 B b=new B();
20 b.test();;
21 A a=new B();
22 b.test();
23 }
24 }
25 ?
26 /*
27 输出结果是:
28 B→test()
29 B→test()
30 */
①多态—同一方法可以根据发送对象的不同而采用多种不同的行为方式
②一个对象的实际类型是确定的,但可以指向对象的引用类型有很多
③多态存在条件
1.有继承关系
2.子类重写父类方法
3.父类引用指向子类对象
④多态注意事项
1.多态是方法的多态,属性没有多态
2.父类和子类有联系,否则有类型转换异常(ClassCastException)
1 //父类
2 public class Pet {
3 public void run(){
4 System.out.println("Pet→run");
5 }
6 }
7 ?
8 //子类
9 public class Dog extends Pet{
10 public void run(){
11 System.out.println("Dog→run");
12 }
13 public void eat(){
14 System.out.println("Dog→eat");
15 }
16 }
17 ?
18 //应用类
19 public class Application {
20 public static void main(String[] args) {
21 //Dog能调用的方法都是自己的或者继承父类的
22 Dog dog1=new Dog();
23 //Pet 父类型,可以指向子类,但不能调用子类独有的方法
24 Pet dog2=new Dog();
25 Object dog3=new Dog();
26 dog1.run();
27 dog2.run();//子类重写了父类的方法,执行子类的方法
28 //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
29 ((Dog) dog2).eat();
30 dog1.eat();
31 }
32 }
33 ?
34 /*
35 输出结果是:
36 Dog→run
37 Dog→run
38 Dog→eat
39 Dog→eat
40 */
①instanceof用于判断一个对象是什么类型
1 public class Application {
2 public static void main(String[] args) {
3 ?
4 //Obejct→Person→Teacher
5 //Obejct→Person→Student
6 ?
7 Object obj=new Student();
8 System.out.println(obj instanceof Student);//true
9 System.out.println(obj instanceof Person);//true
10 System.out.println(obj instanceof Object);//true
11 System.out.println(obj instanceof Teacher);//false
12 System.out.println(obj instanceof String);//false
13 System.out.println("=================================");
14 Person person=new Student();
15 System.out.println(person instanceof Student);//true
16 System.out.println(person instanceof Person);//true
17 System.out.println(person instanceof Object);//true
18 System.out.println(person instanceof Teacher);//false
19 //System.out.println(person instanceof String);//编译出错
20 System.out.println("=================================");
21 Student student=new Student();
22 System.out.println(student instanceof Student);//true
23 System.out.println(student instanceof Person);//true
24 System.out.println(student instanceof Object);//true
25 //System.out.println(student instanceof Teacher);//编译出错
26 //System.out.println(student instanceof String);//编译出错
27 }
28 }
②类型转换注意事项
1.父类引用指向子类的对象
2.把子类转换为父类,向上转型:可以直接使用
3.把父类转换为子类,向上转型:强制转换
4.方便方法的调用,减少重复的代码
①被static修饰的方法和属性在类加载时一起创建
②可以通过类名.属性名或类名.方法名()访问
1 public class Person {
2 //2 赋初值
3 {
4 System.out.println("匿名代码块");
5 }
6 //1 只执行一次
7 static{
8 System.out.println("静态代码块");
9 }
10 //3
11 public Person(){
12 System.out.println("Person构造方法");
13 }
14 ?
15 public static void main(String[] args) {
16 Person person1=new Person();
17 System.out.println("=================");
18 Person person2=new Person();
19 }
20 }
21 ?
22 /*
23 输出结果是:
24 静态代码块
25 匿名代码块
26 Person构造方法
27 =================
28 匿名代码块
29 Person构造方法
30 */
③静态导入包
1 //静态导入包
2 import static java.lang.Math.random;
3 ?
4 public class Test {
5 public static void main(String[] args) {
6 System.out.println(random());//可以直接通过random()使用,不需要Math.random()
7 }
8 }
①abstract修饰符可以用来修饰方法,也可以用来修饰类
②抽象类—不能使用new关键字来创建对象,它是用来让子类继承的
③抽象方法—只有方法的声明,没有方法的实现,它是用来让自来实现的
④抽象类中可以有普通方法,但抽象方法必须在抽象类中
⑤子类继承抽象类,那么必须要实现抽象类中没有实现的抽象方法,否则该子类也要声明为抽象类
⑥抽象类中也可以有构造方法,子类初始化时会调用父类的构造方法
1 //抽象类
2 public abstract class A {
3 public abstract void doNow();
4 public A(){
5 System.out.println("A的构造方法");
6 }
7 }
8 ?
9 //子类
10 public class B extends A{
11 @Override
12 public void doNow() {
13 System.out.println("doNow");
14 }
15 }
16 ?
17 public class C {
18 public static void main(String[] args) {
19 B b=new B();
20 b.doNow();
21 }
22 }
23 ?
24 /*
25 输出结果是:
26 A的构造方法
27 doNow
28 */
①接口就是规范,定义的是一组规则,本质是契约
②接口用interface关键字实现
③接口中定义的方法用public abstract修饰
④接口中定义的变量用public static final修饰
⑤接口不能被实例化,没有构造方法
⑥类可以通过implements实现多个接口,接口中的方法必须重写
1 //接口UserService
2 public interface UserService {
3 void run();
4 }
5 ?
6 //接口TimeZone
7 public interface TimeZone {
8 String TIME="2021-2";
9 String getTime();
10 }
11 ?
12 public class C implements UserService,TimeZone{
13 @Override
14 public String getTime() {
15 return "2021-2";
16 }
17 ?
18 @Override
19 public void run() {
20 System.out.println("C→run");
21 }
22 }
①成员内部类
1 public class Outer {
2 private int id=10;
3 public void out(){
4 System.out.println("这是一个外部类");
5 }
6 ?
7 class Inner{
8 //内部类可以获取外部类的私有属性和方法
9 public void in(){
10 System.out.println("这是一个内部类,id是"+id);
11 }
12 }
13 }
14 ?
15 public class Application {
16 public static void main(String[] args) {
17 Outer outer=new Outer();
18 //通过外部类来实例化内部类
19 Outer.Inner inner=outer.new Inner();
20 inner.in();
21 }
22 }
23 ?
24 /*
25 输出结果是:
26 这是一个内部类,id是10
27 */
②静态内部类
1 public class Outer {
2 private int id=10;
3 public void out(){
4 System.out.println("这是一个外部类");
5 }
6 static class Inner{
7 public void in(){
8 System.out.println("这是一个内部类");
9 }
10 }
11 }
③局部内部类
1 public class Outer {
2 public void method(){
3 class Inner{
4 public void in(){
5 System.out.println("这是一个内部类");
6 }
7 }
8 }
9 }
④匿名内部类
1 public class Outer {
2 public static void main(String[] args) {
3 //没有名字初始化类
4 new Apple().eat();
5 }
6 }
7 ?
8 class Apple{
9 public void eat(){
10 System.out.println("eat");
11 }
12 }
原文:https://www.cnblogs.com/yqsumAll/p/14394712.html