
正文
类和对象java程序代码 java类和对象程序设计
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java求关于一个类和对象的程序。。。。在线等。。急
public class Test {
public static void main(String args[]){
Pen aPen = new Pen();
aPen.setColor("red");
aPen.setLength(123);
aPen.setPrice(123.45f);
aPen.write();
System.out.println(aPen.getColor());
}
}
class Pen{
private String color;
private int length;
private float price;
public Pen() {
}
public Pen(String color, int length, float price) {
super();
this.color = color;
this.length = length;
this.price = price;
}
public void write(){
System.out.println(color);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
相关问答
Q1: java类和对象的问题
对象是类实例化后的结果,如;Circle ccc = new Circle();那么这里的ccc就是一个对象。
而Circle bottom只是声明了一个变量,该变量的类型是Circle。
声明一个变量之后 就可以对变量就是赋值 了,如 bottom = new Circle();,此时bottom就是一个对象了。
可以把对象的声明和赋值放在一起写,即:Circle ccc = new Circle();
也可以分开写,即:Circle ccc ; ccc= new Circle();
Q2: JAVA类与对象相关编程
public class Number {
private int n1;
private int n2;
public Number(){}
public Number(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
/**
* 加方法
* @return
*/
public static int addition(Number n){
int i=0;
i=n.getN1()+n.getN2();
return i;
}
public static int subtration(Number n){
int i=0;
i=n.getN1()-n.getN2();
return i;
}
public static int multiplication(Number n){
int i=0;
i=n.getN1()*n.getN2();
return i;
}
public static double division(Number n){
double i=0;
if(n.getN1()!=0){
i=n.getN1()/n.getN2();
}else{
System.out.println("被除数不可以为0");
}
return i;
}
}
public class test {
public static void main(String[] args) {
Number n=new Number(10,5);
System.out.println("加法类和对象java程序代码:"+Number.addition(n));
System.out.println("减法类和对象java程序代码:"+Number.subtration(n));
System.out.println("乘法类和对象java程序代码:"+Number.multiplication(n));
System.out.println("除法类和对象java程序代码:"+Number.division(n));
}
}
关于类和对象java程序代码和java类和对象程序设计的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







