1, Three characteristics of object-oriented programming
1) packaging
One of the core ideas of object-oriented programming is to encapsulate data and operations on data. Abstract common properties from concrete instances to form general concepts, such as class concepts.
People often talk about the motor vehicle class is a concept formed by extracting common attributes and functions from specific examples. Then a specific car is an example of the motor vehicle class, that is, an object.
An object encapsulates its own data and the operation of these data reasonably and effectively. For example, every car calls "acceleration" and "deceleration" to change its own running speed.
2) inheritance
Inheritance embodies a kind of advanced programming mode to undertake history. The subclass can inherit the properties and functions of the parent class, that is, it inherits the data and operations of the parent class, and at the same time, it can add the unique data and operations of the subclass.
3) polymorphism
Polymorphism is another important feature of object-oriented programming.
There are two kinds of polymorphisms.
One is the operation name polymorphism, that is, there are multiple operations with the same name, but these operations must receive different parameter types.
Another kind of polymorphism is related to inheritance, which means that different behaviors may occur when the same operation is called by different types of objects.
3.2 encapsulation
You can also put another class in other files in the same directory
Two classes are formed by loading. The main class is psvm, and the main method is run (psvm). When running with cmd, "java -- class with main method -"
package com.example; //Two classes are formed by loading. The main class is psvm, and the main method is run (psvm). When running with cmd, "java -- class with main method --" import java.io.PrintStream; public class javahello_2 { public static void main(String[] args) { Circle2 circle; circle=new Circle2(); circle.radius=100; double are=circle.getARE(); System.out.println(are); } } class Circle2 { double radius; double getARE(){ double are=3.14*radius*radius; return are; } }
new main class can be used in main method
class Circle { double radius; double getARE(){ double are=3.14*radius*radius; return are; } public static void main(String[] args) { Circle circle; circle=new Circle(); System.out.println(circle.toString());///Returns an object string } }
class ComputerCircleArea { //Visible in package public static void main(String args[]) { double radius; //radius double area; //The measure of area radius=100; area=3.14*radius *radius; //Calculated area System.out.printf("The radius is%5.3f The area of a circle of:\n%5.3f\n",radius,area); } } public class Circle2{ double radius; //radius double area; //The measure of area private double getArea(){ Circle2 c1; return 3.14*radius*radius; } public static void main(String sf[]) { Circle2 c; c=new Circle2(); //Object c.radius=10; System.out.println(c.getArea()); //Circle2@15db9742 } } class CircleTest{ public static void main(String sf[]) { Circle2 c; c=new Circle2(); //Object c.radius=10; System.out.println(c.getArea()); //Circle2@15db9742 } }
Example
this
Use do while and for loops to calculate 1 + 1 / 2! + 1 / 3! + 1 / 4 Top 10 and
package com.example; ///Jisuanqi1 is for Jisuanqi2 is dowhile output at the same time, the result is the same public class javahello_2 { public static void main(String[] args) { Jisuanqi1 jisuanqi1=new Jisuanqi1(); Jisuanqi2 jisuanqi2=new Jisuanqi2(); double ans2=0,ans1=0; int n=10; for(int i=1;i<=n;i++) { ans1+=1/jisuanqi1.jiecheng(i); /// // System.out.println(jisuanqi1.jiecheng(i)); /// } int i=1; do { ans2+=1/jisuanqi2.jiecheng(i); /// //System.out.println(jisuanqi2.jiecheng(i)); /// i++; }while (i<=n); System.out.println(ans1+" "+ans2); } } class Jisuanqi1{ double a; double ans; double jiecheng(double a){////Must be reassigned this.a=a; this.ans=1; for(int i=1;i<=a;i++) { ans*=i; } return ans; } } class Jisuanqi2{ double a; double ans; int i; double jiecheng(double in){ ans=1; i=1; a=in; do{ ans*=i; i++; }while (i<=a); return ans; } }