简单的java程序题

Python012

简单的java程序题,第1张

public

class

student{

private

string

stuid

private

string

stuname

private

char

stusex

private

int

stuage

/**

*以下是各属性的getter/setter方法

*/

public

void

setstuid(string

stuid){

this.stuid=stuid

}

public

string

getstuid(){

return

stuid

}

public

void

setstuname(string

stuname){

this.stuname=stuname

}

public

string

getstuname(){

return

stuname

}

public

void

setstusex(char

stusex){

this.stusex=stusex

}

public

char

getstusex(){

return

stusex

}

public

void

setstuage(int

stuage){

this.stuage=stuage

}

public

int

getstuage(){

return

stuage

}

/**

*构造方法,构造学生信息

*/

public

student(string

stuid,string

stuname,char

stusex,int

stuage){

this.stuid=stuid

this.stuname=stuname

this.stusex=stusex

this.stuage=stuage

}

public

string

tostring(){//覆盖该类的tostring()方法

stringbuffer

buff=new

stringbuffer()

buff.append("学号:"+stuid)

buff.append("\n姓名:"+stuname)

buff.append("\n性别:"+stusex)

buff.append("\n年龄:"+stuage)

return

buff.tostring()

}

public

static

void

main(string[]

args){

student

stu=new

student("1000","zhangsan",'男',18)

system.out.println

(stu)//打印学生信息

system.out.println

("--修改姓名结果--")

stu.setstuname("lisi")

system.out.println

(stu)

}

}

public class Test {

/**

* @param moneyAmount 商品的总金额

*/

public void test(double moneyAmount){

double preferentialAmount = 0

if(moneyAmount<0){

System.out.println("请输入正确的金额!")

return

}

if(moneyAmount<100){

System.out.println("购买的商品的总金额小于100元没有优惠,支付的金额是:"+moneyAmount)

}else if (moneyAmount>=100 &&moneyAmount<200) {

preferentialAmount = (moneyAmount-100)*0.9

moneyAmount = moneyAmount-preferentialAmount

print(moneyAmount,preferentialAmount)

}else if (moneyAmount>=200 &&moneyAmount<500) {

preferentialAmount = (moneyAmount-200)*0.8

moneyAmount = moneyAmount-preferentialAmount

print(moneyAmount,preferentialAmount)

}else {

preferentialAmount = (moneyAmount-500)*0.7

moneyAmount = moneyAmount-preferentialAmount

print(moneyAmount,preferentialAmount)

}

}

/**

* @param payAmount 支付的金额

* @param preferentialAmount 优惠的金额

*/

public void print(double payAmount,double preferentialAmount){

System.out.println("支付的金额是:"+payAmount+",优惠的金额是:"+preferentialAmount)

}

}

System.out.println("1--" + a1.show(b))

a1是A类引用指向A类对象,不存在多态,一定调用A类方法。A类方法有两个show(D)和show(A),b是B类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),输出A and A。

System.out.println("2--" + a1.show(c))

输出A and A,原因同上。

System.out.println("3--" + a1.show(d))

调用show(D),输出A and D。

System.out.println("4--" + a2.show(b))

a2是A类引用指向B类对象,可能存在多态。b是B类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),而B类重写了show(A),因此调用的是重写后的show(A),输出B and A。

System.out.println("5--" + a2.show(c))

同上,C类引用无法转换为D类引用,但可以转换为A类引用,因此调用show(A),输出B and A。

System.out.println("6--" + a2.show(d))

调用show(D),show(D)又调用父类即A类的show(D),输出A and D

System.out.println("7--" + b.show(b))

b是B类引用指向B类对象,不存在多态,一定调用B类方法。B类一共有三个方法:重写自A类的show(A)和show(D),以及新定义的show(B)。show(b)调用show(B)方法,输出B and B

System.out.println("8--" + b.show(c))

C类继承自B类,也调用show(B)方法,输出B and B

System.out.println("9--" + b.show(d))

调用show(D),show(D)又调用父类即A类的show(D),输出A and D