導航:首頁 > 創造發明 > java創造一個類

java創造一個類

發布時間:2021-09-18 09:33:29

A. JAVA中創建一個Factory類,它具有proce()方法。

public
class
Factory
{
public
void
proce(){
System.out.println("我是無參的proce方法");
}
public
void
proce(int
i){
System.out.println("我是有1個int型的參數proce方法:"+i);
}
public
void
proce(String
name){
System.out.println("我是有一個String參數的proce方法:"+name);
}
public
void
proce(String
name,int
i){
System.out.println("我是有一個int型參數和一個String參數的proce方法:"+name+","+i);
}
public
static
void
main(String
args[]){
Factory
f=new
Factory();
f.proce();
f.proce(2);
f.proce("張三");
f.proce("王五",3);
}
}

B. 用JAVA創建一個學生類

public Class Student{
private String name;
private String age;
private String height;
private String weight;
public Student(String name,String height,String weight){
this.name = name;
this.height = height;
this.weight = weight;
}
public void setAge(String age){
this.age = age;
}
public void out(){
System.out.println(name+" "+age+" "+weight+" "+height);
}
public static void main(String[] args){
Student stu = new Student("test"."180","180");
stu.setAge("26");
stu.out();
}
}

C. java中創建一個類

public class Question {
private String label;//私有全局變數
/**
* 無參構造方法
*/
public Question(){

}
/**
* 有參構造方法
*/
public Question(String label){
this.label = label;
}
//get方法
public String getLabel() {
return label;
}
}

D. 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),可以隨意的訪問外部類的成員,這可讓我們更好地組織管理我們的代碼,增強代碼的可讀性。

E. java 編程創建一個Point類

public class Point {

private double x;
private double y;

//默認構造坐標原點
public Point(){
this.x = 0d;
this.y = 0d;
}

//測試
public static void main(String[] args) {
Point p = new Point();
p.printCurrentPosition();//列印當前位置坐標原點
p.move(22, 33.5);//移動
p.printCurrentPosition();

}

public void move(double newX, double newY){
this.x = newX;
this.y = newY;
}

public void printCurrentPosition(){
System.out.println("Position now is: (" + x + ", " + y + ")");
}

}
-----------testing
Position now is: (0.0, 0.0)
Position now is: (22.0, 33.5)

F. Java創建一個學生類


publicclassTest{

publicstaticvoidmain(String[]args){
ArrayList<Student>studentList=newArrayList<>();
studentList.add(newStudent("張三","class1","Java",85));
studentList.add(newStudent("周樂兒","class1","C#",79));
studentList.add(newStudent("王濤","class2","C#",52));
studentList.add(newStudent("李明","class2","Java",48));
//===========================================================================
System.out.println("姓名 班級名 課程名 考試成績");
for(Studentstudent:studentList){
System.out.println(student);
}
//===========================================================================
floatclass1SumScore=0;
intclass1StudentCount=0;
for(Studentstudent:studentList){
if(student.isClass1()){
class1SumScore+=student.getScore();
class1StudentCount++;
}
}
System.out.println(" class1班的總分:"+class1SumScore+" 平均分:"+class1SumScore/class1StudentCount);
//===========================================================================
floatjavaScore=0;
floatjavaStudentCount=0;
floatcSharpScore=0;
floatcSharpStudentCount=0;
for(Studentstudent:studentList){
if(student.isJava()){
javaScore+=student.getScore();
javaStudentCount++;
}elseif(student.isCSharp()){
cSharpScore+=student.getScore();
cSharpStudentCount++;
}
}
System.out.println(" Java課程的平均分:"+javaScore/javaStudentCount+" C#課程的平均分:"+cSharpScore/cSharpStudentCount);
}
}

classStudent{
privateStringname;
privateStringclassName;
privateStringcourseName;
privateintscore;

publicStudent(Stringname,StringclassName,StringcourseName,intscore){
this.name=name;
this.className=className;
this.courseName=courseName;
this.score=score;
}

publicbooleanisClass1(){
return"class1".equals(className);
}

publicbooleanisJava(){
return"Java".equals(courseName);
}

publicbooleanisCSharp(){
return"C#".equals(courseName);
}

publicStringgetName(){
returnname;
}

publicStringgetClassName(){
returnclassName;
}

publicStringgetCourseName(){
returncourseName;
}

publicintgetScore(){
returnscore;
}

@Override
publicStringtoString(){
returnname+" "+className+" "+courseName+" "+score;
}
}

G. java中創建一個對象

因為java類一般會自帶一個無參數的構造器,
但是如果你顯示的定義了構造器
不管是無參數還是有參數的都會以顯示的為准!
那麼你的程序里剛好有一個顯示的構造器
Bike(int a) {
this.a = a;
}
那麼在實例化在對象時就只能採用該構造器!
構造器也是函數 因此必須正確使用!
也就是說你必須帶一個int型的參數

H. java類的裡面可以再定義一個類嗎

java類裡面還可以定義一個類,即內部類。

  1. java內部類分為: 成員內部類、靜態嵌套類、方法內部類、匿名內部類 。

  2. 內部類的共性

(1)、內部類仍然是一個獨立的類,在編譯之後內部類會被編譯成獨立的.class文件,但是前面冠以外部類的類名和$符號 。

(2)、內部類不能用普通的方式訪問。內部類是外部類的一個成員,因此內部類可以自由地訪問外部類的成員變數,無論是否是private的 。

(3)、內部類聲明成靜態的,就不能隨便的訪問外部類的成員變數了,此時內部類只能訪問外部類的靜態成員變數。

I. 用JAVA建了一個類,怎麼再創建一個子類

看了下拉
大致知道你要干什麼拉
有幾個地方要改
1 public class Donggandidai extends Huadan {

public Donggandidai(){
float ctf=0.6f;
float dxf=0.1f;
float shf=0.25f;
tc="動感地帶的資費是";
hf=ctf*ct+dxf*dx+shf*sh;

}
}
還有2個類也這樣改,就是都改到構造方法裡面去!
2 public class Factory {
public Huadan fangfa4(int i){

Huadan a;
if(i==1)
{
a=new Donggandidai();
}
if(i==2){
a=new Quanqiutong();
}
else{
a=new Shenzhouxing();
}
return a;

}
}
多個判斷用swith case,比較好,你這里上面i==1 等於白判斷了
switch (i){
case 1:a=new Donggandidai();break;
case 2:a=new Quanqiutong(); break;
case 3:a=new Shenzhouxing(); break;
}
return a
再簡單分析下拉
你Factory類的fangfa4方法返回一個Huadan類,你用創建它的子類返回這沒有問題的,你在主方法中用一個Huadan來指向這個子類也沒有問題,這相當於用一個父類的變數指向一個子類的對象,也就是多態!但是這個時候你是不能再調用子類的獨有而父類沒有的方法,而且你沒有定義子類的構造方法,所以父類的2個成員變數hf和tc沒有賦你想賦的值,都只有它們的默認值null和0.0,所以你列印出來的是null0.0,而且你Factory中的fangfa4方法中的判斷也不對,如果判斷比較多,建議用switch case

J. JAVA創建一個具體的學生類

//學生類
public class Student{
private String stu_id;
private String stu_name;
private float math;
private float chinese;
private float computer;

public void setStu_id(String stu_id){
this.stu_id=stu_id;
}
public String getStu_id(){
return stu_id;
}
public void setStu_name(String stu_name){
this.stu_name=stu_name;
}
public String getStu_name(){
return stu_name;
}
public void setMath(float math){
this.math=math;
}
public float getMath(){
return math;
}
public void setChinese(float chinese){
this.chinese=chinese;
}
public float getChinese(){
return chinese;
}
public void setComputer(float computer){
this.computer=computer;
}
public float getComputer(){
return computer;
}

}

//主方法的類
public class Start{
public static void main(String[] args){
Student stu1=new Student();
stu1.setStu_id("No1");
stu1.setStu_name("張三");
stu1.setMath(89.5f);
stu1.setChinese(70f);
stu1.setComputer(98f);
Student stu2=new Student();
stu2.setStu_id("No2");
stu2.setStu_name("李四");
stu2.setMath(79.5f);
stu2.setChinese(90f);
stu2.setComputer(68f);
float stu1Sum = stu1.getMath()+stu1.getChinese()+stu1.getComputer();
float stu2Sum = stu2.getMath()+stu2.getChinese()+stu2.getComputer();
if(stu1Sum > stu2Sum){
System.out.println ("總分最高分為:"+stu1Sum);
System.out.println ("學號:"+stu1.getStu_id());
System.out.println ("姓名:"+stu1.getStu_name());
System.out.println ("數學:"+stu1.getMath());
System.out.println ("語文:"+stu1.getChinese());
System.out.println ("計算機:"+stu1.getComputer());
}else if(stu1Sum < stu2Sum){
System.out.println ("總分最高分為:"+stu2Sum);
System.out.println ("學號:"+stu2.getStu_id());
System.out.println ("姓名:"+stu2.getStu_name());
System.out.println ("數學:"+stu2.getMath());
System.out.println ("語文:"+stu2.getChinese());
System.out.println ("計算機:"+stu2.getComputer());
}else{
System.out.println ("總分一樣高為:"+stu1Sum);
System.out.println ("學號:"+stu1.getStu_id());
System.out.println ("姓名:"+stu1.getStu_name());
System.out.println ("數學:"+stu1.getMath());
System.out.println ("語文:"+stu1.getChinese());
System.out.println ("計算機:"+stu1.getComputer());
System.out.println ("學號:"+stu2.getStu_id());
System.out.println ("姓名:"+stu2.getStu_name());
System.out.println ("數學:"+stu2.getMath());
System.out.println ("語文:"+stu2.getChinese());
System.out.println ("計算機:"+stu2.getComputer());
}
}
}

希望對你有幫助!

閱讀全文

與java創造一個類相關的資料

熱點內容
榮玉證書 瀏覽:382
凌文馬鞍山 瀏覽:34
石柱鎮工商局 瀏覽:854
鋼鐵發明國 瀏覽:118
創造與魔法怎麼賣人民幣 瀏覽:101
知識產權專題答案 瀏覽:760
高發明巫溪 瀏覽:755
衛生室公共衛生服務考核標准 瀏覽:493
亞洲給水排水有版權嗎 瀏覽:397
湖北省醫療糾紛預防與處理辦法 瀏覽:230
星光創造營後勤在哪 瀏覽:581
北京辦理知識產權 瀏覽:177
交通銀行信用卡有效期是幾年 瀏覽:913
公司協議股權轉讓 瀏覽:531
啥叫擔保物權 瀏覽:60
馬鞍山到徐州的火車 瀏覽:703
羊年限定金克絲多少錢 瀏覽:573
公共基本衛生服務結核項目試題 瀏覽:896
寶雞市工商局電話號碼 瀏覽:81
基本公共衛生服務督導工作方案 瀏覽:454