Java中的具體類
什麼是Java中的具體類?
具體類是最初為其所有相關方法宣告的基本常規實現。此類沒有任何未實現的方法,也可以擴充套件到抽象類或介面類。我們可以使用 new 關鍵字建立物件類。該過程可以繼承另一個抽象類。此過程也稱為 Java 中存在的 100% 實現類。
Java中具體類的優缺點
Java Java 中的具體類有很多優點和缺點:
優點
此過程用於分解複雜設計。
減少物件依賴性。
它是多個實現的即時注入。
可以擴充套件到介面和抽象類。
缺點
不如其他方法靈活,但速度更快。
本質上未列出。
定義Java中具體類的演算法
在這個可能的演算法中,我們將向您展示如何在 Java 環境中宣告和執行具體類。透過使用此演算法,我們將構建一些可能的語法以有效地解釋問題陳述。
步驟 1 - 開始該過程。
步驟 2 - 宣告和匯入 Java 包。
步驟 3 - 宣告一個公共類。
步驟 4 - 宣告一個字串名稱。
步驟 5 - 返回值。
步驟 6 - 提及抽象方法。
步驟 7 - 繼承之前的類。
步驟 8 - 宣告主方法。
步驟 9 - 宣告字串引數。
步驟 10 - 列印所需的值。
步驟 11 - 獲取返回值。
步驟 12 - 終止該過程。
定義Java中具體類的語法
class ConcreteCalculator { static int add(int a, int b){ return a + b; } static int subtract(int a, int b){ Concrete Class in Java return a - b; } static int multiply(int a, int b){ return a * b; } static int divide(int a, int b){ return a / b; } public static void main(String[] args){ //Declaration of main method int sum = add(Value, Value); int diff = subtract(Value, Value); int prod = multiply(Value, Value); int div = divide(Value, Value); //Print the values of the process System.out.println("" + sum); System.out.println("" + diff); System.out.println("" + prod); System.out.println("" + div); } } abstract class Shape{ abstract double area(); abstract double perimeter(); } class Circle extends Shape{ double r = 5; public double area(){ return 3.14 * r * r; } public double perimeter(){ return 2 * 3.14 * r; } } class Triangle extends Shape{ double a = 5; double b = 12; double c = 9; double h = 4; public double area(){ return b * h / 2; } public double perimeter(){ return a + b + c; } } class Square extends Shape{ double a = 8; public double area(){ return a * a; } public double perimeter(){ return 4 * a; } }
在上面可能的語法中,我們試圖向您展示如何宣告和建立具體類。透過使用這種可能的語法,我們將朝著可能的 Java 示例前進,以便從更廣泛的角度理解問題陳述。
遵循的方法
方法 1 - 使用 sum 類來說明具體類的 Java 程式,也使用此類定義一個 product 類
方法 2 - 使用具體方法定義抽象類的 Java 程式,並使用具有實用程式方法的 ShapeUtil 類
方法 1:說明具體類和產品類
sum 類方法的使用
在這種可能的方法中,我們將應用 sum() 類方法,使用 Java 中的具體類來獲取加法值。
public class XYZ extends Main class implements ABC { public String honk() { return "beep"; } }
示例
//Java program to illustrate concrete class by using the product and sum classes public class ARBRDD { static int product(int a, int b){ return a * b; } static int sum(int a, int b){ return a + b; } public static void main(String args[]){ int p = product(16, 7); int s = sum(7, 16); System.out.println("Product: " + p); System.out.println("Sum: " + s); } }
輸出
Product: 112 Sum: 23
Product 類方法的使用
在這種可能的方法中,我們將應用 product() 類方法,使用 Java 中的具體類來獲取乘積值。
示例
//Java program to illustrate concrete class by implementing a product() method in an interface interface X { int product(int a, int b); int sum(int a, int b); } abstract class Product implements X { public int product(int a, int b){ return a * b; } } public class Main extends Product { public int sum(int a, int b){ return a + b; } public static void main(String args[]){ Main ob = new Main(); int p = ob.product(16, 7); int s = ob.sum(7, 16); System.out.println("Product of the two numbers is: " + p); System.out.println("Sum of the two numbers is: " + s); } }
輸出
Product of the two numbers is: 112 Sum of the two numbers is: 23
方法 2:使用具體方法和 ShapeUtil 類定義抽象類
抽象類方法的使用
在這種可能的方法中,我們將應用抽象類方法,使用 Java 中的具體類來獲取所需的值。
public void draw() { System.out.println("A Statement Value..."); } public double getArea() { return width * height; }
示例
//Java program to define an abstract class by using a concrete method abstract class Shape{ abstract double area(); abstract double perimeter(); } class Circle extends Shape{ double r = 5; public double area(){ return 3.14 * r * r; } public double perimeter(){ return 2 * 3.14 * r; } } class Triangle extends Shape{ double a = 5; double b = 12; double c = 9; double h = 4; public double area(){ return b * h / 2; } public double perimeter(){ return a + b + c; } } class Square extends Shape{ double a = 8; public double area(){ return a * a; } public double perimeter(){ return 4 * a; } } public class DemoMain { public static void main(String[] args) { Circle c = new Circle(); double c_area = c.area(); double c_perimeter = c.perimeter(); System.out.println("Area of Circle: " + c_area); System.out.println("Perimeter of Circle: " + c_perimeter); Triangle t = new Triangle(); double t_area = t.area(); double t_perimeter = t.perimeter(); System.out.println("Area of Triangle: " + t_area); System.out.println("Perimeter of Triangle: " + t_perimeter); Square s = new Square(); double s_area = s.area(); double s_perimeter = s.perimeter(); System.out.println("Area of Square: " + s_area); System.out.println("Perimeter of Square: " + s_perimeter); } }
輸出
Area of Circle: 78.5 Perimeter of Circle: 31.400000000000002 Area of Triangle: 24.0 Perimeter of Triangle: 26.0 Area of Square: 64.0 Perimeter of Square: 32.0
ShapeUtil 類方法的使用
在這種可能的方法中,我們將應用具有實用程式方法的 ShapeUtil 類來繪製任何形狀並使用具體類進行列印。
示例
//Java program to use a ShapeUtil Class that has utility methods to draw any shapes and print with a concrete class abstract class Shape { private String name; public Shape() { this.name = "Unknown shape is here"; } public Shape(String name) { this.name = name; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public abstract void draw(); public abstract double getArea(); public abstract double getPerimeter(); } class Rectangle extends Shape { private double width; private double height; public Rectangle(double width, double height){ super("Rectangle Is The Aimed Drawing"); this.width = width; this.height = height; } public void draw() { System.out.println("Drawing a rectangle..."); } public double getArea() { return width * height; } public double getPerimeter() { return 2.0 * (width + height); } } class Circle extends Shape { private double radius; public Circle(double radius) { super("Circle is the aimed drawing"); this.radius = radius; } public void draw() { System.out.println("Drawing a circle..."); } public double getArea() { return Math.PI * radius * radius; } public double getPerimeter() { return 2.0 * Math.PI * radius; } } class ShapeUtil { public static void drawShapes(Shape[] list){ for (int i = 0; i < list.length; i++){ list[i].draw(); } } public static void printShapeDetails(Shape[] list){ for (int i = 0; i < list.length; i++){ String name = list[i].getName(); double area = list[i].getArea(); double perimeter = list[i].getPerimeter(); System.out.println("Name of the drawing: " + name); System.out.println("Area of the drawing: " + area); System.out.println("Perimeter value is: " + perimeter); } } } public class Main{ public static void main(String[] args){ Shape[] shapeList = new Shape[2]; shapeList[0] = new Rectangle(16.0, 7.0); shapeList[1] = new Circle(23.0); ShapeUtil.drawShapes(shapeList); ShapeUtil.printShapeDetails(shapeList); } }
輸出
Drawing a rectangle... Drawing a circle... Name of the drawing: Rectangle Is The Aimed Drawing Area of the drawing: 112.0 Perimeter value is: 46.0 Name of the drawing: Circle is the aimed drawing Area of the drawing: 1661.9025137490005 Perimeter value is: 144.51326206513048
結論
具體類是 Java 程式的普通類,其中不包含任何抽象方法。在今天的文章中,我們學習了 Java 中的具體類及其相關方法。使用上述演算法和語法;我們構建了一些 Java 程式碼以有效的方式解釋問題陳述。
另請閱讀: Java 面試問題及答案