javapeople类的简单介绍
今天给各位分享javapeople类的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、Java定义People类,它具有以下成员变量:String name,int age,它有两个构造方法!
- 2、JAVA 定义Person类
- 3、写一个Java编程
- 4、Java把类people改为接口people?
- 5、java 定义一个类people属性域
- 6、java问题people.java
Java定义People类,它具有以下成员变量:String name,int age,它有两个构造方法!
public class People {
private String name;
private Integer age = 20;
public People(String name, Integer age) {
System.out.println("A 1");
this.name = name;
this.age = age;
}
public People(String name) {
System.out.println("A 2");
this.name = name;
new People(name, age);
}
public void work(){
System.out.println("A work");;
}
}
public class Teacher extends People{
public Teacher(String name) {
super(name);
}
public void work(){
System.out.println("T are teach");
}
}
public class Student extends People{
public Student(String name) {
super(name);
}
public void work(){
System.out.println("Student are work");
}
}
public class D {
public static void main(String[] args) {
Student s = new Student("s1");
s.work();
Teacher t = new Teacher("t1");
t.work();
}
}
JAVA 定义Person类
public class Person {
private String name;
private int age;
Person(String name ,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name="+name+",age="+age;
}
}
public class Student extends Person {
private String id;
private String school;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
Student(String name, int age,String id ,String school) {
super(name, age);
this.id = id;
this.school=school;
}
public void Hello (){
System.out.println("Hello"+ this.school + super.getName());
}
@Override
public String toString() {
return "school="+this.school+",id="+this.id+","+super.toString();
}
}
public class Test {
public static void main(String[] args) {
Person person = new Person("hanmeimei", 21);
System.out.println(person);
Student student = new Student("Lilei", 22, "ID01", "希望小学");
System.out.println(student.toString());
}
}
写一个Java编程
package yourPackageName;
import java.math.BigDecimal;
public class People {
// (1)成员变量:name、height、weight分别表示姓名、身高(cm)和体重(kg)。
// 姓名
private String name;
// 身高(cm)
private int height;
// 体重(kg)
private int weight;
/**
* 构造方法通过参数实现对成员变量的赋初值操作
*/
People(String name, int height, int weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
/**
* 该方法返回0、1、-1分别表示标准、过胖或过瘦)。 判断方法是:用身高减去110作为参考体重,超过参考体重5kg以上的,为“过胖”;
* 低于参考体重5kg以上的 ,为“过瘦”;在(参考体重-5kg)和(参考体重+5kg)之间的,为“标准”。
*/
int check() {
int standard = this.height - 110;
if (this.weight standard + 5) {
return 1;
}
if (this.weight standard - 5) {
return -1;
}
return 0;
}
@Override
public String toString() {
return new StringBuffer(this.name).append(",").append(this.height).append("cm,").append(this.weight).append("kg,").append(this.check()).toString();
}
/**
* (4)在main方法中,输入50个学生的信息(姓名、身高和体重),分别输出标准、过胖或过瘦的人数(必须通过调用check()方法实现)。
*
* @return void
*/
public static void main(String[] args) {
int aCount = 0;
int bCount = 0;
int cCount = 0;
for (int i = 0; i 50; i++) {
// 创建50个身材体重随机的People
People man = new People("People" + i, new BigDecimal(Math.random() * 50).intValue() + 150, new BigDecimal(Math.random() * 60).intValue() + 40);
switch (man.check()) {
case 0:
aCount++;
break;
case 1:
bCount++;
break;
case -1:
cCount++;
break;
}
}
System.out.println("标准身材:" + aCount + "个");
System.out.println("过胖:" + bCount + "个");
System.out.println("过瘦:" + cCount + "个");
}
}
Java把类people改为接口people?
其他的好说,people接口中,定义公共的 void speakHello(); 子类实现,并扩展自己特有的方法即可,但是 weight,height的考察点在哪???在接口中,变量相当于 直接被public final static 修饰且实例化了,也就是说没法修改,那么可以直接在实现类中定义。
java 定义一个类people属性域
People类:
import java.util.Date;
public class People {
String name;
Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public People()
{
}
public People(String s,Date date)
{
this.name=s;
this.birthday=date;
}
public void getAge()
{
Date today=new Date();
int age=today.getYear()-getBirthday().getYear();
System.out.println("age = "+age);
}
/**
* @param args
*/
public static void main(String[] args) {
People p1=new People();
p1.setBirthday(new Date(87,1,2));
p1.getAge();
People p2=new People("xiaoming",new Date(90,1,2));
p2.getAge();
}
}
运行结果:
age = 23
age = 20
Student类:
import java.util.Date;
public class Student extends People {
String stuNo;
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public Student(String s,Date date,String no)
{
super(s,date);
this.stuNo=no;
}
public void readStuNo()
{
System.out.println("stuNo = "+getStuNo());
}
/**
* @param args
*/
public static void main(String[] args) {
Student p2=new Student("xiaoming",new Date(90,1,2),"20101000");
p2.getAge();
p2.readStuNo();
}
}
运行结果:
age = 20
stuNo = 20101000
java问题people.java
1.在java规范中
public static void main(String args[])只能放在public类中
你的main函数放在A类中,而A类不是public的。
2.java规范还规定,一个.java文件中只能有一个public类,而且该类类名必须和文件名相同。
3.java类成员默认为protected,也就是保护的。应该在所有的字段和函数前加上public.
修改方法(以下两种任选一种)
一、1。去掉A类,把main函数放到peoeple类里面。
2。float hight,weight; 前加public
String head,ear,mouth; 前加public
void speak(String s) 前加public
二、象二楼说的:
1。文件名改为A.java
2。public class peoeple改为class peoeple
3。class A改为public class A
2。float hight,weight; 前加public
String head,ear,mouth; 前加public
void speak(String s) 前加public
另外,几点建议:
1.你的peoeple应该是people
2.people里应该有一个字段 String name;
3.增加构造函数
public people(String myname)
{
name=myname;
}
4.speak函数体加上System.out.println(name+"说:"+s);
关于javapeople类和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。