java创建一个类
㈠ 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{}
㈡ 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类,如下
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如何创建一个类
定义一个类,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),可以随意的访问外部类的成员,这可让我们更好地组织管理我们的代码,增强代码的可读性。
㈤ 怎么用java写一个类
一个java标准类的必备要素如下:
//定义类的归属,即包名
packagecom.test;
//定义类名,public关键字为可选,不过如果为public,则源文件必须与类名一致.
publicclassTest{
//定义类属性
privateintid;
//构造方法
publicTest(intid){
this.id=id;
}
//get,set方法
publicvoidsetId(intid){
this.id=id;
}
publicintgetId(){
returnthis.id;
}
//自定义方法
publicvoiddoMethod(){
//TODO方法的返回值和参数,依据业务逻辑
}
}
//以上为一个最简化的java类.可以增加静态方法,比如程序入口
㈥ java如何定义一个类,创建它的成员变量和方法
建立一个Javaproject——点右键新建一个类,类名字最好是大写开头,LZ 我给你写一个简单的类x0dx0apublic class Test{x0dx0a//定义成员变量x0dx0aint width=10;x0dx0aint height=10;x0dx0a// 成员方法x0dx0apublic area(){x0dx0a return width*height ;x0dx0a }x0dx0a}
㈦ java中如何以给定的字符串为名字创建某个类的实例
可以用反射根据给定的类名来动态生成实例
比如你定义了一个类
packagesample;
/**
*Createdbypseudoon15-9-16.
*/
publicclassTestClass{
privateStringname;
publicTestClass(Stringname){
this.name=name;
}
@Override
publicStringtoString(){
return"TestClass{"+
"name='"+name+'''+
'}';
}
}
然后使用如下代码创建这个类的一个实例
publicstaticvoidmain(String[]args)throwsException{
//加载要反射的类
Classclz=Class.forName("sample.TestClass");
//找到类的构造方法
Constructorconstructor=clz.getDeclaredConstructor(String.class);
//使用找到的构造方法创建实例
Objectinstance=constructor.newInstance("Lilei");
System.out.println(instance);
}这个instance就是需要的结果
㈧ 怎么用"java"写一个类
class B{\x0d\x0a private int a;//声明变量\x0d\x0a public B()//构造函数\x0d\x0a{\x0d\x0a}\x0d\x0apublic void setA(int a)//设置a的值\x0d\x0a{\x0d\x0a this.a=a;\x0d\x0a\x0d\x0a}\x0d\x0apublic int getA()//获取a的值\x0d\x0a{\x0d\x0areturn a;\x0d\x0a}\x0d\x0apublic public static void main(String[] args)//必须要的主函数\x0d\x0a{\x0d\x0aB b=new B();//建立一个B的对象b\x0d\x0ab.setA(3);//调用b对象里的方法setA();\x0d\x0aSystem.out.println(b.getA);//输出a\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}
㈨ 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类
//希望对你有帮助
㈩ java创建一个A类和B类
完整代码:
interface C{
}
interface D{
}
// 操作用户
class A implements C{
// 保存客户对象
List<Customer> customers = new ArrayList<>();
// 保存客户数量
int numOfCus;
// 添加用户
public void addCustomer(){
// 可以手动添加用户
this.customers.add(new Customer("张","三"));
this.customers.add(new Customer("李","四"));
}
// 查找用户
public boolean loadCustomer(){
for (int i = 0; i < customers.size(); i++) {
if(customers.get(i) == null){
return false;
}
}
return true;
}
// 返回当前客户数量
public int numCustomer(){
numOfCus = customers.size();
return numOfCus;
}
// 删除用户
public void deleteCustomer(){
customers.clear();
}
}
// 客户
class Customer implements D{
String firstName;
String lastName;
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
// 测试
public class Customer_Test {
public static void main(String[] args) {
A a = new A();
// 添加对象
a.addCustomer();
// 查询用户
boolean b = a.loadCustomer();
System.out.println("当前存在用户吗? " + b);
// 用户数量
int num = a.numCustomer();
System.out.println("当前有" + num + "个用户");
// 删除用户
a.deleteCustomer();
System.out.println("用户已被删除");
num = a.numCustomer();
System.out.println("现在用户数量是:" + num);
}
}
测试效果:

