当前位置:首页 » 编程语言 » java新建类

java新建类

发布时间: 2022-10-07 02:51:32

1. java建立一个类

帮你写个看看:


/**
*人类
*有‘年龄’,‘性别’,‘身高’,‘体重’,‘职业’属性
*@author
*
*/
publicclassPerson{
protectedintage;//年龄
protectedStringsex;//性别
protectedStringhigh;//身高
protectedStringweight;//体重
protectedStringskill;//职业

publicPerson(){
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}

publicStringgetSex(){
returnsex;
}

publicvoidsetSex(Stringsex){
this.sex=sex;
}

publicStringgetHigh(){
returnhigh;
}

publicvoidsetHigh(Stringhigh){
this.high=high;
}

publicStringgetWeight(){
returnweight;
}

publicvoidsetWeight(Stringweight){
this.weight=weight;
}

publicStringgetSkill(){
returnskill;
}

publicvoidsetSkill(Stringskill){
this.skill=skill;
}

publicStringtoString(){

return"年龄:"+age+""+"性别:"+sex+""+"身高:"+high+""+"体重:"+weight+""+"职业:"+skill;
}
}

/**
*教师类有‘学历’属性,‘收入’属性
*
*@authorAdministrator
*
*/
{
privateStringecation;//学历
privateStringincome;//收入

publicTeacher(){
}

publicStringgetEcation(){
returnecation;
}

publicvoidsetEcation(Stringecation){
this.ecation=ecation;
}

publicStringgetIncome(){
returnincome;
}

publicvoidsetIncome(Stringincome){
this.income=income;
}

publicStringtoString(){

returnsuper.toString()+""+"学历:"+ecation+""+"收入:"
+income;
}
}

/**
*学生类有‘学历’‘是否在谈恋爱’属性
*
*@authorAdministrator
*
*/
{
privateStringecation;//学历
privatebooleanisInLove;//是否在谈恋爱

publicStudent(){
}

publicStringgetEcation(){
returnecation;
}

publicvoidsetEcation(Stringecation){
this.ecation=ecation;
}

publicbooleanisInLove(){
returnisInLove;
}

publicvoidsetInLove(booleanisInLove){
this.isInLove=isInLove;
}

publicStringtoString(){

returnsuper.toString()+""+"学历:"+ecation+""+"是否在谈恋爱:"
+isInLove;
}
}

/**
*官员类
*有收入属性
*@authorAdministrator
*
*/
{
privateStringincome;//收入

publicOfficial(){
}

publicStringgetIncome(){
returnincome;
}

publicvoidsetIncome(Stringincome){
this.income=income;
}

publicStringtoString(){

returnsuper.toString()+""+"收入:"+income;
}
}


/**
*测试类
*@authorAdministrator
*
*/
publicclassTest{
publicstaticvoidmain(String[]args){
Personperson=newPerson();
person.setSex("男");
person.setAge(20);
person.setHigh("1.70m");
person.setWeight("128kg");
person.setSkill("神经病");
System.out.println("人类-"+person);//打印人

Teacherteacher=newTeacher();
teacher.setSex("男");
teacher.setAge(30);
teacher.setHigh("1.80m");
teacher.setWeight("128kg");
teacher.setSkill("教师");
teacher.setIncome("月薪10000");
teacher.setEcation("本科");
System.out.println("教师-"+teacher);//打印老师

Studentstudent=newStudent();
student.setSex("女");
student.setAge(10);
student.setHigh("1.60m");
student.setWeight("98kg");
student.setSkill("学生");
student.setEcation("小学");
student.setInLove(true);
System.out.println("学生-"+student);//打印学生


Officialofficial=newOfficial();
official.setSex("人妖");
official.setAge(50);
official.setHigh("1.40m");
official.setWeight("158kg");
official.setSkill("村长");
official.setIncome("月薪100000");
System.out.println("官员-"+official);//打印官员

}
}
//把每个类放到单独的java文件里面去
//运行Test类
//希望对你有帮助

2. 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();
}

}
你明白了吗?

3. java类创建方法

这哪是什么类创建方法啊.就是一个方法.这个方法的作用是创建一个OrderModel 并赋值该类相应的字段.

public OrderModel getOrderModel()
//方法名为:getOrderModel,返回类型OrderModel
{

OrderModel orderModel = new OrderModel();
//新建一个OrderModel

orderModel.setOrderId(orderId);
orderModel.setDate(date);
orderModel.setShipped(shipped);
orderModel.setPurchases(purchases); //设置相应字段

return orderModel; //返回新建的orderModel
}

4. 如何在eclipse中新建Java类

步骤如下:
选中工程里面的src文件夹, 右键鼠标--选择new -- 选择class。
在弹出框中输入class name 。
点击OK即可添加类了。

针对问题中的具体要求, 新建了java工程后,可以直接复制Person.java文件, 然后选择java工程的src目录,按ctrl + v 进行粘贴。就可以把Person.java代码粘贴到eclipse中去了, eclipse默认是自动编译的 ,添加的java代码会自动编译成class字节码文件

5. 如何在eclipse中新建Java类

1.在工程src包下 右键 选择“new”- package ,一般情况下,我们需要新建一个包来包括java类
2.输入你要建立的包的名称
3.创建包完毕
4.在包上右键“new”-"class"
5.这里填写你新建类的名称,选择public 如果要主函数的话,选择下边的 public static void main(String[] args),点击finish
6.新建完毕的类

6. 新建java终态类步骤

咨询记录 · 回答于2021-11-16

7. 如何在eclipse中新建Java类

1.在工程src包下 右键 选择“new”- package ,一般情况下,我们需要新建一个包来包括java类

2.输入你要建立的包的名称,一般是以com靠头,公司域名居中,然后用途结尾,例如 com.. 就是层的一个包

3.创建包完毕

4.在包上右键“new”-"class"

5.这里填写你新建类的名称,选择public 如果要主函数的话,选择下边的 public static void main(String[] args),点击finish
6.新建完毕的类

8. 如何在eclipse中新建Java类

第一步:启动Eclipse,第一次启动软件时,会提示选择工作空间(WorkSpace),可在硬盘上新建一个文件夹作为工作空间,这个文件夹将会保存你所编写的所有的源代码。第二步:新建一个简单的Java工程。依次点file->new->project->javaproject。第三步:别是填写工程名以及源码文件夹,一般情况是默认src作为根文件夹。第四步:在工程中新建一个包。值得注意的是包名和工程名一般都是小写开头,而Java类名则是大写开头。右键单击src,依次点击new->package->填写包名。第五步:在包中新建一个Java类,一个包中可以有多个Java类,Java类是我们写代码的区域。在上一步中新建的包上右键单击,依次选择new->class,填写类名完成。

9. Java如何创建一个类

定义一个类,class A={},然后创建这个类对象,A a = new A();有没有参数看你类定义的构造函数; 例代码如下:
class TestInner{
public static void main(String [] args)
{
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
}
}
在内部类(Inner Class),可以随意的访问外部类的成员,这可让我们更好地组织管理我们的代码,增强代码的可读性。

10. java中怎样创建一个类有那些方法,请举例说明.谢谢!

1、定义一个类,class A={},然后创建这个类对象,A a = new A();有没有参数看你类定义的构造函数;
2、继承java已有的类,以异常类为例,class A extends Exception{}
3、实现java已有接口或者抽象类,class A implements Runnable{}
4、单继承多实现,class A extends B implements C,D{}

热点内容
云服务器最先跑什么 发布:2025-07-01 05:02:39 浏览:215
微信公众平台手机上传视频 发布:2025-07-01 04:58:53 浏览:414
数控车床螺纹编程实例 发布:2025-07-01 04:46:27 浏览:421
什么是安卓品牌尖货 发布:2025-07-01 04:45:50 浏览:696
写入设备缓存 发布:2025-07-01 04:37:35 浏览:431
小鸡g4怎么连安卓 发布:2025-07-01 04:37:25 浏览:355
黄金线主图源码 发布:2025-07-01 04:35:38 浏览:297
阿里轻量服务器有固定IP吗 发布:2025-07-01 04:30:27 浏览:506
电脑什么配置要合适 发布:2025-07-01 04:24:15 浏览:166
哪个安卓恢复工具不要钱 发布:2025-07-01 04:21:54 浏览:743