• 使用typora编辑博客


    使用typora编辑博客(测试)

    第一部分

    • 使用代码块
     [post.jfif](..Picturespost.jfif) package interfacedemo;
    /**
     * @classname:Interf
     * @description:
     * (1)定义shape接口,包含求面积和求周长的方法。 (2)定义Circle类、Rectangle类、Square类。 
     * (3)要求Circle类和Rectangle类实现shape接口,
     * Square类继承Rectangle类。 运行时,让用户选择输入什么图形,然后输入相应数据,求出该图形的面积和周长。
     *
     */
    import java.util.Scanner;
    
    interface shape{                           //接口定义
    	double getArea();
    	double getCircumference();
    }
    class Circle implements shape{
         private final double PI=3.14; 
    	private double radius;
    	public Circle(double r)
    	{radius=r;}
    	
                                 //抽象方法实现
    	public double getArea() {                          //一个类在实现接口的抽象方法时,!必须!显式使用public修饰符
    		// TODO Auto-generated method stub
    		
        	 return PI*radius*radius;
    	}
    
    	
    	public double getCircumference() {			       //一个类在实现接口的抽象方法时,!必须!显式使用public修饰符
    		// TODO Auto-generated method stub
    		return 2*PI*radius;
    	}	
    }
    
    class Rectangle implements shape{
    	protected double width;
    	protected double height;
    	Rectangle(){}
    	Rectangle(double w,double h)
    	{
    		width=w;
    		height=h;
    	}
    	public double getArea() {
    		return width*height;
    	}
    	
    	public double getCircumference() {
    		return 2*(width+height);
    	}
    }
    
    class Square extends Rectangle{
    	double side;
    	Square(double s)
    	{this.width=s;
    	this.height=s;
    	}
    }
    
    
    
    public class Interf {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
          Scanner sc=new Scanner(System.in);          //Scanner 输入
          
          System.out.println("将要输入的图形为:");
          String name =sc.next();
         if(name.equals("圆形"))
            {System.out.println("请输入半径:");
        	 double r=sc.nextDouble();
        	 
        	 System.out.println("圆形的周长为:"+new Circle(r).getCircumference()+" 面积为:"+new Circle(r).getArea());   //匿名
            }
         else if(name.equals("矩形"))
            {System.out.println("请输入长和宽:");
        	 double x,y;
        	 x=sc.nextDouble();
        	 y=sc.nextDouble();
            Rectangle rec=new Rectangle(x,y);          																	//非匿名
            System.out.println("矩形的周长为:"+rec.getCircumference()+" 面积为:"+rec.getArea());
            }
         else if(name.equals("正方形"))
            {System.out.println("请输入边长:");
        	double s=sc.nextDouble() ;
        	Square squa=new Square(s);
        	System.out.println("正方形周长为:"+squa.getCircumference()+" 面积为:"+squa.getArea());
    }
         else System.out.println("输入错误!");
           
         sc.close();		
    	}
    
    }
    
    

    第二部分

  • 相关阅读:
    python模块之linecache
    如何在cmd命令下运行python脚本
    Git remote: ERROR: missing Change-Id in commit message
    Git命令git update-index --assume-unchanged,忽略不想提交的文件(忽略跟踪)
    Git命令cherry-pick,选择把一部分代码提交到另一个分支
    Redis可以用来做什么?(摘自http://www.lianpenglin.cc廉鹏林博客)
    Yii笔记:打印sql、Form表单、时间插件、Mysql的 FIND_IN_SET函数使用、是否是post/ajax请求
    Yii1使用Gii生成模块实现CURD
    通过经纬度获取所属城市信息-php
    树莓派进阶之路 (010)
  • 原文地址:https://www.cnblogs.com/potofsalt/p/13561096.html
Copyright © 2020-2023  润新知