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 面试问题及答案
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP