导航:首页 > 编程语言 > 写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一个类相关的资料

热点内容
大连php培训学校 浏览:985
怎么指定定向流量app的免流 浏览:900
华为云服务器有啥软件 浏览:654
礼记正义pdf 浏览:988
CorePDF 浏览:733
python多文件调用 浏览:329
linux如何用python 浏览:188
超易学的python 浏览:159
控制面板命令行 浏览:51
为什么空气难压缩是因为斥力吗 浏览:643
郭天祥单片机实验板 浏览:601
服务器有什么危害 浏览:258
饥荒怎么开新的独立服务器 浏览:753
文件夹变成了 浏览:560
linuxpython绿色版 浏览:431
怎么下载小爱同学音箱app 浏览:554
python占位符作用 浏览:76
javajdbcpdf 浏览:543
php网页模板下载 浏览:192
python试讲课pygame 浏览:409