java Three characteristics of :

encapsulation : The commonness of things belonging to the same category ( Include properties and methods ) Into a class , For ease of use .
1:
concept : Encapsulating data and data-based operations with abstract data types , Make it an indivisible and independent entity , Data is protected inside abstract data types , Hide interior details as much as possible , Only some external interfaces are reserved for external connection .
2:
benefit :(1) Realized the division of labor . After encapsulating the code that can realize a specific function into a separate entity , Programmers can call it when they need it , So as to realize the division of labor .(2) Hide information , Implementation details . By controlling access rights, you can hide information that you don't want client programmers to see , If the password of a customer's Bank needs to be kept secret , You can only develop permissions for this customer .

inherit : Individual's acceptance of common attributes and methods , And add the unique attributes and methods of personality .
1. concept : One class inherits another , The inherited class is called a subclass , The inherited class is the parent class .
2. objective : Realize code reuse .
3. understand : The relationship between children and parents is not the relationship between father and son in daily life , The relationship between subclass and parent is a kind of specialization and generalization , yes is-a Relationship of , Subclasses are more detailed classifications of parent classes . as class
dog extends animal, It can be understood as dog is a
animal. Pay attention to design inheritance , To enable a class to inherit , The parent class needs to be properly open for access , Following the principle of Riemannian substitution , That is, close to modify and open to extension , That is to say, open - Closed principle .
4. result : After inheritance, the child class automatically owns the properties and methods of the parent class , But in particular , The private property and constructor of the parent class cannot be inherited .
Other subclasses can write their own special properties and methods , The purpose is to extend the function , Subclass can also copy the method of the parent class, that is, method rewriting .

polymorphic : The concept of polymorphism developed , Is based on encapsulation and inheritance .

Polymorphism is to implement a unified behavior at the abstract level , To individual ( specific ) On the level of , This unified behavior will be based on the individual ( specific ) To carry out one's own characteristic behavior .( For an abstract thing , For internal individuals, they can find their own behaviors to perform .)
1. concept : The same thing , Call the same method , When the parameters are the same , But the behavior is different .

2. understand : Children appear as parents , But do things in your own way . The child class needs to be transformed upward when it appears as a parent class (upcast), Among them, the upward transformation is from JVM Automatic , It's safe , But a downward transformation (downcast) It's not safe , Cast required . When a subclass appears as a parent, its own properties and methods cannot be used .

use java Make a simple calculator ;

import java.io.*;
class OperationAttridute{
private double numberA=0; private double numberB=0; private double result=0;
public double setNumberA(double i) { return numberA=i; } public double
getNumberA() { return numberA; } public double setNumberB(double j) { return
numberB=j; } public double getNumberB() { return numberB; } public double
setResult(double z) { return result=z; } public double getResult(double
a,double b) { return result; }
}

// To be calculated 2 Number and operation result are encapsulated .

class OperationA extends OperationAttridute
{
public double getResult(double a,double b) { double result=0; result=a+b;
return result; }
}

// Addition class : inherit OperationAttridute Class and override its getResult method

class OperationS extends OperationAttridute
{
public double getResult(double a,double b) { double result=0; result=a-b;
return result; }
}

// Subtraction class : inherit OperationAttridute Class and override its getResult method

class OperationM extends OperationAttridute
{
public double getResult(double a,double b) { double result=0; result=a*b;
return result; }
}

// multiplicative : inherit OperationAttridute Class and override its getResult method

class OperationD extends OperationAttridute
{
public double getResult(double a,double b) { double result=0; if(b==0) {
Throw new RuntimeException(“ Dividend cannot be 0”); } result=a/b; return result; }
}

// Division class : inherit OperationAttridute Class and override its getResult method , Division to judge the dividend and throw an exception

class Operationdo
{
public static Object expression_r(char r)
{
OperationAttridute oa=new OperationAttridute(); switch(r) { case '+': oa=new
OperationA(); break; case '-': oa=new OperationS(); break; case '*': oa=new
OperationM(); break; case '/': oa=new OperationD(); break; } return oa; }
}

// Operation class : Parameters passed in , To call the operation method

class Account

{
public static void main(String []args)throws Exception
{
char r=’*’;
OperationAttridute oa=(OperationAttridute)Operationdo.expression_r(r); double
a=oa.setNumberA(9); double b=oa.setNumberB(2);
System.out.println(oa.getResult(a,b)); }
}

// main program
such , Just enter the operation symbol , Through polymorphism , The way to return the parent class implements the result of the calculator .

Technology
©2019-2020 Toolsou All rights reserved,
JAVA Detailed explanation of anomalies MySQL An interview is a must ! How to use it quickly html and css Write static page R Language cluster analysis case Dialogue between apple and Nissan suspended ,Apple Car How's it going ?java Realize the function of grabbing red packets SpringBoot practice ( five ):mybatis-plus In BaseMapper,Iservice and ServiceImpl Google says home office affects work efficiency !2021 Return to offline office in 2010 about keras use fit_generator Encountered in StopIteration Programmer Tanabata Valentine's Day confession code