java程序设计 面向对象基础 实验

java程序设计 面向对象基础 实验,第1张

二。

1。Peoplejava

public class People {

//性别(sex)及出生日期(date);方法成员有:获取人的性别和出生日期及构造方法。要求构造方法可以设置性别和出生日期的初始值。

private int sex;

private Date birth;

public People (int sex, Date birth) {

thissex = sex;

thisbirth = birth;

}

public int getSex() {

return thissex;

}

public Date getBirth() {

return thisbirth;

}

}

2。Studentjava

public class Student extends People{

private int sex;

private Date birth;

private String name;

private int stuno;

private double grate;

private String studentNative;

public Student(int sex, Date birth, String name, int stuno, double grate, String studentNative) {

super(sex, birth);

thisname = name;

thisstuno = stuno;

thisgrate = grate;

thisstudentNative = studentNative;

}

public Date getBirth() {

return birth;

}

public double getGrate() {

return grate;

}

public String getName() {

return name;

}

public int getSex() {

return sex;

}

public String getStudentNative() {

return studentNative;

}

public int getStuno() {

return stuno;

}

}

3。测试类自己编写就好了,创建一个People和Student的对象,然后设置值,取值就可以了。

五。

1构造方法没有返回值,方法名和类名一样

2继承是指子类可以拥有父类的方法和属性;多态是指父类的引用可以指向子类对象的实例

3重写

4重载

其他的没什么了,自己完成以下吧。O(∩_∩)O~

你可以这样理解:类变量相当于一个人的姓,是先于对象存在的;而实例变量是对象创建之后再存在的,相当于人的名字;

1、类变量也叫静态变量,也就是在变量前加了static 的变量;

2、实例变量也叫对象变量,即没加static 的变量;

3、区别在于:类变量和实例变量的区别在于:类变量是所有对象共有,其中一个对象将它值改变,其他对象得到的就是改变后的结果;而实例变量则属对象私有,某一个对象将其值改变,不影响其他对象;

直接赋值粘贴

public class Commodity {

private String type;

private String noun;

private double salePrice;

private String unit;

private String size;

private double percent;

public Commodity(String type, String noun, double salePrice, String unit, String size, double percent) {

super();

thistype = type;

thisnoun = noun;

thissalePrice = salePrice;

thisunit = unit;

thissize = size;

thispercent = 1;

}

public double getSalePrice() {

return salePrice;

}

public void setSalePrice(double salePrice) {

thissalePrice = salePrice;

}

public double getPercent() {

return percent;

}

public void setPercent(double percent) {

thispercent = percent;

}

@Override

public String toString() {

return "Commodity [type=" + type + ", noun=" + noun + ", salePrice=" + salePrice + ", unit=" + unit + ", size="

+ size + ", percent=" + percent + "]";

}

}

请采纳,谢谢

以下SQL语句基于MySQL数据库实现:

1,在数据库中建立一个表,表名为学生,其结构为:学号、姓名、性别、年龄、成绩。

create table student

(

num char(4),

name char(10),

sex char(6),

age int,

score float

);

2,在学生表中输入4条记录

insert into student values ('0001','Susan','Female',19,89);

insert into student values ('0002','Merry','Female',20,87);

insert into student values ('0003','Tom','Male',18,44);

insert into student values ('0004','Jack','Male',20,90);

3,将每人的成绩增加10%

update student set score = score + score01;

4,将每条记录按照成绩由大到小的顺序显示到屏幕上

select from student order by score desc;

5,删除成绩不及格的学生记录

delete from student where score < 60;

用我的,我的绝对比上面那个写得好。(至少用了线程同步synchronized)

import javatextSimpleDateFormat;

import javautilArrayList;

import javautilDate;

import javautilIterator;

public class Account {

private String name;

private double balance; // 余额

private int id;

private ArrayList<Transaction> transactions;

public Account(String name, int id, double balance) {

thisname = name;

thisbalance = balance;

thisid = id;

thistransactions = new ArrayList<Transaction>();

}

//用同步方法,防止多线程 *** 作导致帐户余额出错

public synchronized void withDraw(double money) throws Exception {

if (money > balance) {

throw new Exception("帐户余额不足!");

} else {

balance -= money;

transactionsadd(new Transaction("Withdraw", money));

}

}

//用同步方法,防止多线程 *** 作导致帐户余额出错

public synchronized void deposit(double money) {

balance += money;

transactionsadd(new Transaction("Deposit", money));

}

//返回此帐户的所有信息,包括过往存取款纪录

public String checkAccount() {

String msg = "您好!" + name + " 先生,您的帐户余额为: " + balance + " RMB" + "\n"

+ "银行存取纪录:\n";

if (transactionssize() == 0)

return msg + "无";

else {

Iterator it = transactionsiterator();

while (ithasNext())

msg += itnext()toString() + "\n";

return msg;

}

}

/

@param args

/

public static void main(String[] args) {

// TODO Auto-generated method stub

Account account = new Account("你妹", 1, 0);

accountdeposit(100000);//存10W

//取2次随机数的款项

for (int i = 0; i < 2; i++) {

double money;

money = Mathrandom() 1000;

try {

accountwithDraw(money);

} catch (Exception e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

//打印帐户所有纪录

Systemoutprintln(accountcheckAccount());

}

}

class Transaction {

private Date createdDate; //交易产生的日期

private String operation; //交易的 *** 作

private double money; //交易钱款

public Transaction(String opertaion, double money) {

createdDate = new Date();

thisoperation = opertaion;

thismoney = money;

}

@Override

public String toString() {

return "[" + new SimpleDateFormat("yyyy-MM-dd")format(createdDate)

+ " " + operation + " " + money + " RMB]";

}

}

以上就是关于java程序设计 面向对象基础 实验全部的内容,包括:java程序设计 面向对象基础 实验、在Java程序设计中实例变量和类变量有什么区别、java的程序设计等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9479936.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存