導航:首頁 > 編程語言 > 寫java一個類

寫java一個類

發布時間:2022-07-25 13:05:03

① 怎樣在java中寫一個類,調用可執行jar包求編程大神!


/**
*
*@authorkaifang
*@date2017年12月5日下午4:43:45
*/
publicclassTest22{
publicstaticvoidmain(String[]args){
try{
//運行jar包程序「textencode.jar」,需要運行那個改成那個jar包名稱即可
Runtime.getRuntime().exec("java-jar"+"textencode.jar");
}catch(IOExceptione){
e.printStackTrace();
}
}
}

這種方式很多此一舉,最簡單的是使用批處理建立.bat文件,里邊寫:

java -jar textencode.jar

雙擊就可以運行jar包程序了

② JAVA寫一個類.

public class Rectangle {
private int length;
private int width;

public Rectangle() {

}

public Rectangle(int length, int width) {

this.length = length;
this.width = width;
}

public int getLength() {
return length;
}

public void setLength(int length) {
this.length = length;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public String toString() {
return "長度:" + length + "寬度:" + width;
}

public void perimeter() {
System.out.println("周長是"+(length + width) * 2);
}

public void area() {
System.out.println("面積是:"+length * width);
}

public static void main(String[] args) {
Rectangle r=new Rectangle(100,200);
System.out.println(r);
r.perimeter();
r.area();
}
}

③ 用Java語言寫一個類

class
Student
{
private
String
sno;
private
int
age;
private
String
name;
private
String
grade;
public
Student(){}
public
Student(String
sno,
int
age,
String
name,
String
grade)
{
this.sno
=
sno;
this.age
=
age;
this.name
=
name;
this.grade
=
grade;
}
public
String
getSno()
{
return
sno;
}
public
void
setSno(String
sno)
{
this.sno
=
sno;
}
public
int
getAge()
{
return
age;
}
public
void
setAge(int
age)
{
this.age
=
age;
}
public
void
setName(String
name)
{
this.name
=
name;
}
public
String
getName()
{
return
name;
}
public
String
getGrade()
{
return
grade;
}
public
void
setGrade(String
grade)
{
this.grade
=
grade;
}
public
void
showSno(){
System.out.println("
學號
為:"+this.getSno());
}
public
void
showAge(){
System.out.println("
年齡為:"+this.getAge());
}
public
void
showName(){
System.out.println("
姓名為:"+this.getName());
}
public
void
showGrade(){
System.out.println("
班級為:"+this.getGrade());
}
}
public
class
TestStudent{
public
static
void
main(String
args[]){
Student
s1=new
Student("001",20,"張三","07(4)");
s1.showName();
s1.showAge();
s1.showSno();
s1.showGrade();
}
}

④ 用java編寫一個類,

import java.util.ArrayList;
import java.util.List;

class Course{
String courseName;
List<String> studentName;
Course(String courseName){
this.courseName = courseName;
this.studentName = new ArrayList<String>();
}

public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public List<String> getStudentName() {
return studentName;
}
public void setStudentName(List<String> studentName) {
this.studentName = studentName;
}

}
/**
*
* @author ww
*
*/
public class TestCourse {

/**
*
* @param args
*/
public static void main(String[] args) {
Course course1 = new Course("高數");
course1.getStudentName().add("張三");
course1.getStudentName().add("李四");
course1.getStudentName().add("趙五");

Course course2 = new Course("英語");
course2.getStudentName().add("張三");
course2.getStudentName().add("李四");
course2.getStudentName().add("劉六");

System.out.println("課程名:"+course1.getCourseName());
System.out.print("學生名單:");
for(String studentName:course1.getStudentName()){
System.out.print(studentName+"\t");
}
System.out.println();

System.out.println("課程名:"+course2.getCourseName());
System.out.print("學生名單:");
for(String studentName:course2.getStudentName()){
System.out.print(studentName+"\t");
}
System.out.println();
}

}

⑤ java 定義類 如何寫

先定義個介面
public
interface
interf{
piblic
void
i();
}
再定義個你想要寫功能的實現類
將來換它就行
public
class
aimpl
implements
interf{
public
void
i(){
這里是你實現的功能
}
}
最後回到c類上
public
class
c{
public
code
(){
interf
in=new
aimpl();
in.i();
}
}
這樣就ok了
以後只要將aimpl這個文件換掉功能也就變了

⑥ java中寫一個類,是叫定義一個類還是叫聲明定義一個類呢

叫什麼無所謂,一般是說定義。
C語言里分聲明和實現兩種,但java沒有這種區分。

⑦ java寫一個類

public class Test{
public static void main(String[] args) {
Student p1 = new Student("小明",22,178.6);
p1.study();
Worker w1 = new Worker("張三", 33, 82.2);
w1.working();
}
}
class Person{
protected String name;
protected int age ;
Person(){

}
Person(String name , int age){
this.name = name ;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void walk(){
System.out.println("...Person...walking...");
}
}
//
class Student extends Person{
private double height ;
Student(){
}
Student(String name,int age,double height){
super(name,age);
this.height = height;
}
public void setHeight(double height){
this.height = height;
}
public double getHeight(){
return height ;
}
public void walk(){
System.out.println("...Student...walking...");
}
public void study(){
System.out.println(this.name+"正在學習,"+this.name+"已經有"+this.age+"歲了,"+this.name+"的身高是"+this.height+".");
}
}
//
//
class Worker extends Person{

private double weight;
Worker(){
}
Worker(String name,int age,double weight){
super(name,age);
this.weight= weight;
}
public void setWeight(double weight){
this.weight= weight;
}
public double getWeight(){
return weight;
}
public void walk(){
System.out.println("...Worker...walking...");
}
public void working(){
System.out.println(this.name+"正在工作,"+this.name+"已經有"+this.age+"歲了,"+this.name+"+人的體重是+"+this.weight+"公斤.");
}

}

閱讀全文

與寫java一個類相關的資料

熱點內容
圖像壓縮編碼實現 瀏覽:66
特色功能高拋低吸線副圖指標源碼 瀏覽:69
西方哲學史pdf羅素 瀏覽:872
python最常用模塊 瀏覽:182
溫州直播系統源碼 瀏覽:110
程序員在上海買房 瀏覽:382
生活解壓游戲機 瀏覽:907
季羨林pdf 瀏覽:716
php支付寶介面下載 瀏覽:814
ipad怎麼把app資源庫關了 瀏覽:301
量柱比前一天多源碼 瀏覽:416
電子書app怎麼上傳 瀏覽:66
國家反詐中心app注冊怎麼開啟 瀏覽:804
全波差分傅里葉演算法窗長 瀏覽:41
程序員如何講自己做過的項目 瀏覽:7
程序員要看的書頸椎 瀏覽:946
php文章cms 瀏覽:553
CSS權威指南第三版PDF 瀏覽:496
android怎麼搭建框架 瀏覽:184
正宗溯源碼大燕條一克一般多少錢 瀏覽:917