「构建java类」java什么是类,如何创建类
今天给各位分享构建java类的知识,其中也会对java什么是类,如何创建类进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、如何创建JAVA类,如下?
- 2、Java如何创建一个类?
- 3、myeclipse 中怎么样新建一个java的类的,详细的步骤是什么?谢谢!
- 4、java如何创建一个类的对象
- 5、java如何定义一个类,创建它的成员变量和方法?
- 6、java建立一个类
如何创建JAVA类,如下?
1)new---project---Java Project -- Project Name 中写工程
2) public class Mammal{
public int weight; //类型根据需要定
protected int height;
int legs;
private int tail;
public void printWeight() {
System.out.println(“ The weight is: ”+weight);
}
protected void printHeight() {
System.out.println(“ The heigh tis: ” + height);
}
void printLegs(){
System.out.println(“ The tail tis: ”+ tail);
}
private void printTail(){
System.out.println(“ The legs tis: ”+ legs);
}
}
3) public class Cat extends Mammal{
public void printWeight() {
System.out.println(“ The weight of the cat is: ”+weight);
}
}
哎, 没有动力了, 不想写了, 先给分吧, 有动力再写吧
Java如何创建一个类?
比如老师给你个作是,定义一个人类,人类下面有学生和老师。你可以这样子定义,然后又一个Test类,来实现老师,学生,人类之间的关系。。
Public class Person{
}
class Student{
}
class Teacher{
}
class Test{
public static void main(String arg[]){
//写内容。。
}
}
myeclipse 中怎么样新建一个java的类的,详细的步骤是什么?谢谢!
myeclipse中新建Java类步骤如下:
1、点击左上角“File”,鼠标移到“new”,点击“Java Project”
2、在“Project name”处填入文件名称,也就是项目名
3、找到刚才新建的项目,双击打开
4、在“src”右击,选择“new”子菜单里面的“Class”
5、给新建的Java类取名,例如Hello
6、点击下方的Finish
7、可以看到新的Hello类已经建好
8、去本地磁盘检验一下,看“Hello”类是否已建好,下图就说明成功啦
扩展资料:
java类的基本结构
属性:对象数据的描述
方法:对象的行为
构造方法:用于实例化对象
内部类:在类中声明的类(inner class)
块:分静态块与实例块
类的声明:(访问权限修饰符public.default(可忽略不写,为默认))(修饰符final.abstract.synchronized)class 类名{ 类体 }
类的作用:类就是一个模板,定义多个对象共同的属性和方法 如:学生类(张三,李四) 手机类(华为.oppo)
java如何创建一个类的对象
Java创建一个类的对象通过new关键字创建:
语法如下:
类 对象 = new 类构造方法();
实例:
public class Person{
String name;
char sex;
int age;
public static void main(String[] args){
//在main方法完成Person类的对象person创建
Person person1 = new Person();
}
}
你明白了吗?
java如何定义一个类,创建它的成员变量和方法?
建立一个Javaproject——点右键新建一个类,类名字最好是大写开头,LZ 我给你写一个简单的类\x0d\x0apublic class Test{\x0d\x0a//定义成员变量\x0d\x0aint width=10;\x0d\x0aint height=10;\x0d\x0a// 成员方法\x0d\x0apublic area(){\x0d\x0a return width*height ;\x0d\x0a }\x0d\x0a}
java建立一个类
帮你写个看看:
/**
* 人类
* 有『年龄』,『性别』,『身高』,『体重』,『职业』属性
* @author
*
*/
public class Person {
protected int age;// 年龄
protected String sex;// 性别
protected String high;// 身高
protected String weight;// 体重
protected String skill;// 职业
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHigh() {
return high;
}
public void setHigh(String high) {
this.high = high;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
public String toString(){
return "年龄:" + age +" " +"性别:" + sex + " " + "身高:" +high + " " +"体重:"+weight + " "+ "职业:"+skill;
}
}
/**
* 教师类 有『学历』属性,『收入』属性
*
* @author Administrator
*
*/
public class Teacher extends Person {
private String education;// 学历
private String income;// 收入
public Teacher() {
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getIncome() {
return income;
}
public void setIncome(String income) {
this.income = income;
}
public String toString() {
return super.toString() + " " + "学历:" + education + " " + "收入:"
+ income;
}
}
/**
* 学生类 有『学历』『是否在谈恋爱』属性
*
* @author Administrator
*
*/
public class Student extends Person {
private String education;// 学历
private boolean isInLove;// 是否在谈恋爱
public Student() {
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public boolean isInLove() {
return isInLove;
}
public void setInLove(boolean isInLove) {
this.isInLove = isInLove;
}
public String toString() {
return super.toString() + " " + "学历:" + education + " " + "是否在谈恋爱:"
+ isInLove;
}
}
/**
* 官员类
* 有收入属性
* @author Administrator
*
*/
public class Official extends Person {
private String income;// 收入
public Official() {
}
public String getIncome() {
return income;
}
public void setIncome(String income) {
this.income = income;
}
public String toString() {
return super.toString() + " " + "收入:" + income;
}
}
/**
* 测试类
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
Person person = new Person();
person.setSex("男");
person.setAge(20);
person.setHigh("1.70m");
person.setWeight("128kg");
person.setSkill("神经病");
System.out.println("人类 -" + person);//打印人
Teacher teacher = new Teacher();
teacher.setSex("男");
teacher.setAge(30);
teacher.setHigh("1.80m");
teacher.setWeight("128kg");
teacher.setSkill("教师");
teacher.setIncome("月薪10000");
teacher.setEducation("本科");
System.out.println("教师 -" + teacher);//打印老师
Student student = new Student();
student.setSex("女");
student.setAge(10);
student.setHigh("1.60m");
student.setWeight("98kg");
student.setSkill("学生");
student.setEducation("小学");
student.setInLove(true);
System.out.println("学生 -" + student);//打印学生
Official official = new Official();
official.setSex("人妖");
official.setAge(50);
official.setHigh("1.40m");
official.setWeight("158kg");
official.setSkill("村长");
official.setIncome("月薪100000");
System.out.println("官员 -" + official);//打印官员
}
}
//把每个类放到单独的java文件里面去
//运行Test 类
//希望对你有帮助
关于构建java类和java什么是类,如何创建类的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-26,除非注明,否则均为
原创文章,转载请注明出处。