//用到了冒泡排序
import javautilArrayList;
import javautilList;
public class Main {
public static void main(String[] args) {
Person A = new Person("A", 178, "男", 18);
Person B = new Person("B", 156, "女", 18);
Person C = new Person("C", 171, "男", 20);
Person D = new Person("D", 181, "男", 25);
Person E = new Person("E", 166, "女", 19);
Person F = new Person("F", 173, "男", 17);
Person G = new Person("G", 160, "女", 20);
Person H = new Person("H", 174, "男", 21);
Person I = new Person("I", 169, "男", 19);
Person J = new Person("J", 158, "女", 17);
List<Person> list = new ArrayList<Person>();
listadd(A);
listadd(B);
listadd(C);
listadd(D);
listadd(E);
listadd(F);
listadd(G);
listadd(H);
listadd(I);
listadd(J);
Person temp;
for (int k = 0; k < listsize(); k++) {
for (int j = 0; j < listsize() - k - 1; j++) {
if (listget(j)getHeight() > listget(j + 1)getHeight()) {
temp = listget(j);
listset(j, listget(j + 1));
listset(j + 1, temp);
}
}
}
for (Person person : list) {
Systemoutprintln(person);
}
}
}
class Person {
/ 姓名 /
private String name;
/ 身高 /
private int height;
/ 性别 /
private String gender;
/ 年龄 /
private int age;
public Person(String name, int height, String gender, int age) {
thisname = name;
thisheight = height;
thisgender = gender;
thisage = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + ", gender=" + gender + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
thisheight = height;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
thisgender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
thisage = age;
}
}
运行结果:
/
类的关键字是:class
继承的关键字是:Student
@author qd
/
public class Person {
String name;
int age;
}
// 学生
class Student extends Person {
double score;
}
// 研究生
class Undergaduate extends Student {
String degree;
}
class Persion
{
public:
char name[20];
int age;
Persion()
{
}
// 这个一般情况下应该是虚函数,否则可能会内存泄漏
virtual ~Persion()
{
}
}
class Student: public Persion
{
public:
// 学号
int Xh;
Student()
{
}
// 这个一般情况下应该是虚函数,否则可能会内存泄漏
virtual ~Student()
{
}
}
class Teacher: public Persion
{
public:
// 职称
int Zc;
Student()
{
}
// 这个一般情况下应该是虚函数,否则可能会内存泄漏
virtual ~Student()
{
}
}
class Person{
private String name;
private int age;
public Person(String name){
thisname=name;
}
public Person(String name, int age){
thisname=name;
thisage =age;
}
//get/set省略
public void show(){
Systemoutprintln("name:"+name+", age:"+age);
}
}
我顺手写了下,参考看看吧。讲道理作业还是要自己做啊,练习练习总是好的。
class Program{
static void Main(string[] args)
{
Person jack = new Person("Jack", "男", 1805, 615);
Person rose = new Person("Rose", "女", 1716, 514);
jackShowPersonInfo();
roseShowPersonInfo();
SystemConsoleRead();
}
}
public class Person
{
private string name;
private string gender;
private double height;
private double weight;
public Person()
{
}
public Person(string strName, string strGender, double strHeight, double strWeight)
{
name = strName;
gender = strGender;
height = strHeight;
weight = strWeight;
}
public void ShowPersonInfo()
{
SystemConsoleWriteLine("姓名:{0}, 性别:{1}, 身高:{2}, 体重:{3}", name, gender, height, weight);
}
}
以上就是关于创建一个Person类,有以下属性:名字、身高、性别、年龄,创建10个对象,用java程序全部的内容,包括:创建一个Person类,有以下属性:名字、身高、性别、年龄,创建10个对象,用java程序、Java中定义“类”的关键字是什么请写出定义一个“人“Person的程序片段, 其包含的属性有: 姓名和年龄、C++ 在一个程序中,类Person 定义了name和age 两个数据成员;等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)