Object oriented programming (2)

Java passers by 2021-01-20 22:56:34
object oriented programming


object-oriented programming ( in )

inheritance (inheritance)

​ When the same properties and behaviors exist in multiple classes , Extract these contents into a single class in , So many classes no longer need to define these properties and behaviors , Just inherit that class

Multiple classes are called subclasses ( Derived class ), The individual class is called the parent class ( Base or superclass )

effect :

  • Inheritance reduces code redundancy , Improved code reusability
  • The emergence of inheritance , More conducive to the expansion of functions
  • The emergence of inheritance creates a relationship between classes , It provides the premise of polymorphism

Class inherits syntax rules :

class Subclass name extends Parent class name { }

rewrite (override/overwrite)

​ In subclasses, you can modify the methods inherited from the parent class as needed , Also known as the method of Reset 、 Cover . When the program is executed , The subclass's method will override the parent's method

requirement :

  • The method overridden by a subclass must have a Same method name 、 parameter list

  • Subclass overridden method The return value type cannot be greater than Of the method whose parent class is overridden return type

  • Subclass overridden Method cannot use less than Access to the method whose parent class is overridden

    • A child class cannot override a parent class declared as private The method of authority
  • Subclass method The exception thrown cannot be greater than Exception of overridden method of parent class

Methods with the same name and parameter in a subclass and a parent class must be declared as non static Of ( It's rewriting ), Or both static Of ( It's not rewriting ). because static Methods belong to classes , A subclass cannot override a method of a parent class

keyword :super

  • super Can be used to access the attribute
  • super Can be used to call the Member method
  • super Can be used in The constructor for calling the parent class in the subclass constructor

When a member with the same name appears in a child parent class , It can be used super Indicates that a member in the parent class is being called
super The traceability of is not limited to the immediate parent
this References representing objects of this class ,super The identifier representing the memory space of the parent class
When there is no constructor with null arguments in the parent class , The constructor of a subclass must pass the this( parameter list ) perhaps super( parameter list ) Statement to call the corresponding constructor in this class or parent class . meanwhile , Can only ” A choice ” , And must be placed on the first line of the constructor

this and super The difference between

polymorphism

  • Polymorphism of objects : The reference of the parent class points to the object of the child class

  • Java There are two types of reference variables : Compile time types and runtime types

    • Compile time types are declared by the This variable is determined by the type used
    • The runtime type is determined by the object actually assigned to the variable
  • Compile time , Look to the left ; Runtime , Look to the right

    • “ Look to the left ” : Look at the reference of the parent class ( There is no subclass specific method in the parent class )
    • “ Look to the right ” : We're looking at subclass objects ( What is actually running is the subclass overriding the parent class's method )

Virtual method call (Virtual Method Invocation)

Methods with the same name and parameter as the parent class are defined in the subclass , In the case of polymorphism , The method of the parent class is called Virtual methods , The parent class is based on the different subclass objects assigned to it , Dynamically call the method belonging to the subclass . Such a method call cannot be determined at compile time

Person e = new Student();
e.getInfo(); // call Student Class getInfo() Method

The operator :instanceof

x instanceof A: test x Is it a class A The object of , The return value is boolean type

Object type conversion (Casting )

  1. Automatic type conversion : Small data types can be automatically converted to large data types
  2. Cast : You can cast large data types (casting) Small data types
int a=(int)1200L;

Yes Java Object is called a cast modelling

  • Type conversion from subclass to superclass can be done automatically
  • The type conversion from the parent class to the child class must be through the shape ( Cast ) Realization
  • Conversion between reference types without inheritance is illegal
  • It can be used before modeling instanceof Operator to test the type of an object

Object class

  • Object Class is all Java The root parent of class

  • If it is not used in the declaration of a class extends Keyword indicates its parent class , The default parent class is java.lang.Object class

    method(Object obj){…} // You can take any class as its argument
    

== Operator and equals Method

==:

  • Basic type comparison value : As long as the values of two variables are equal , That is to say true
  • Reference types compare references ( Whether to point to the same object ): Only when pointing to the same object ,== To return to true

use “==” When comparing , The data types on both sides of the symbol must be compatible ( Except for basic data types that can be automatically converted ), Otherwise, compilation error

equals(): Sure rewrite

  • Only reference types can be compared , Its function and “==” identical , Compare whether to point to the same object
  • Format :obj1.equals(obj2)
  • When used equals() When comparing methods , Antitype File、String、Date And the packaging (Wrapper Class) Come on , yes Comparison type and content Regardless of whether the same object is referenced

reason : Overridden in these classes Object Class equals() Method
In any case ,x.equals(null), Always return is “false”

toString()

  • toString() Method in Object Definition in class , Its The return value is String type , Returns the class name and its reference address
  • It's going on String When connecting with other types of data , Automatically call toString() Method
  • Basic type data is converted to String Type , The corresponding wrapper class is called toString() Method

Packaging

For eight basic data types, define the corresponding reference types — Packaging ( Wrapper class )
JDK1.5 after , Support automatic packing , Automatic dismantling . But the type must match

 Packaging (Wrapper)

Basic data types are packaged as instances of wrapper classes --- Packing

int i = 500;
Integer t = new Integer(i);
Long l = new Long(“asdf”); //NumberFormatException

Get the basic type variable of wrapper in wrapper class object --- Unpacking

// Call the .xxxValue() Method :
boolean b = bObj.booleanValue();

String conversion to basic data type

// Through the constructor of the wrapper class :
int i = new Integer(“12”);
// By wrapping the class parseXxx(String s) Static methods :
Float f = Float.parseFloat(“12.1”);

The basic data type is converted to a string

// Calling string overloaded valueOf() Method :
String fstr = String.valueOf(2.34f);
// In a more direct way :
String intStr = 5 + “”;
版权声明
本文为[Java passers by]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210120225019044J.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云