Java面向对象编程有哪些基础练习可以学习?
- 内容介绍
- 文章标签
- 相关推荐
本文共计236个文字,预计阅读时间需要1分钟。
编写Java应用程序,其中包含梯形类和主类。要求如下:
(1)梯形类具有属性:上底、下底、高和面积。具有计算面积的功能,并在构造方法中对上底、下底和高进行初始化。
(2)主类创建梯形对象,并调用其计算面积的方法。
public class Trape { float upBottom,downBottom,height; Trape(){ upBottom=3.2f; downBottom=3.2f; height=3.4f; } float s(){ return (upBottom+downBottom)*height/2; } } package gh; public class text2 { public static void main(String[] args) { Trape myTrape=new Trape(); System.out.println(myTrape.s()); } }
本文共计236个文字,预计阅读时间需要1分钟。
编写Java应用程序,其中包含梯形类和主类。要求如下:
(1)梯形类具有属性:上底、下底、高和面积。具有计算面积的功能,并在构造方法中对上底、下底和高进行初始化。
(2)主类创建梯形对象,并调用其计算面积的方法。
public class Trape { float upBottom,downBottom,height; Trape(){ upBottom=3.2f; downBottom=3.2f; height=3.4f; } float s(){ return (upBottom+downBottom)*height/2; } } package gh; public class text2 { public static void main(String[] args) { Trape myTrape=new Trape(); System.out.println(myTrape.s()); } }

