Java学习
2021/8/11 20:06:41
本文主要是介绍Java学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
第24天
构造方法代码实例
//filename: App7_3. java
class Cylinder
{
private double radius;
private int height ;
private double pi= 3. 14;
private String color;
public double setCylinder(double r, int h)
{
radius= r;
height= h;
return r + h;
}
public void setCylinder(String str)
{color= str;
}
public void show( )
{System. out. println(" 圆柱的颜色为: " + color);
}
double area()
{
return pi * radius * radius;
}
double volume()
{
return area() * height;
}
public class App7_3
{
public static void main(String[] args)
{
double r_h;
Cylinder volu=new Cylinder();
r_h=volu. setCylinder(2.5,5);
volu.setCy1 inder("红色");
Systom. out. pintIn(+r_h);
System. out, println(" 圆柱体体积="volu.volume());
volu. show();
}
}
——————————分割线————————————
//filename:App7_4. java
class Cylinder
{private double radius;
private int height;
private double pi=3.14;
public Cylinder(double r,int h)
{
radius= r;
height= h;
}
double area()
{return pi* radius * radius;
}
double volume()
{
return area() * height;
}
public class App7_4
{
public static void main(String[] args)
{
Cylinder volu= new Cylinder(3.5,8);
System. out. println("圆柱底面积="+ volu. area());
System. out. println( "圆柱体体积=”+ volu. volume());
}
}
———————————分割线————————————
//filename: App7_5. java
class Cylinder
{private double radius;
private int height;
private double pi=3.14;
String color;
public Cylinder()
{
radius = 1;
height= 2;
color="绿色";}
public Cylinder(double r,int h,string str)
{radius =r;
height=h;
color=str;
}
public void setColor()
{System. out println"圆柱颜色味:" + color);
}
double area()
{
return pi * radius * radius;
}
double volume()
{
return area() * height;
}
public class App7_5
{
public static void main(String[] args)
{
Cylinder volul = new Cylinder();
System. out. println("圆柱1底面积="+ volu1. area());
volu2. setColor();
}
}
这篇关于Java学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略