當前位置:首頁 » 編程語言 » java創建一個類

java創建一個類

發布時間: 2023-01-30 06:44:37

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);
}
}

測試效果:

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:582
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:876
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:571
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:757
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:673
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1000
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:244
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:103
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:795
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:701