
正文
java精美代码示例 java 代码简洁
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java编程 定义一个Person类,
下面是一个Java代码示例,实现了以上要求:
```java
// 定义Person类,具有身高/体重/年龄等属性
class Person {
int height;
int weight;
int age;
// 构造函数
public Person(int height, int weight, int age) {
this.height = height;
this.weight = weight;
this.age = age;
}
// 输出身高、体重、年龄信息的方法
public void display() {
System.out.println("身高:" + height + "cm");
System.out.println("体重:" + weight + "kg");
System.out.println("年龄:" + age + "岁");
}
// 打招呼的方法
public void say() {
System.out.println("大家好,我是一名普通人。");
}
}
// 由Person类派生出Usa类,增加爱好属性,覆盖父类方法
class Usa extends Person {
String hobby;
// 构造函数
public Usa(int height, int weight, int age, String hobby) {
// 调用父类的构造函数
super(height, weight, age);
this.hobby = hobby;
}
// 覆盖父类的display方法,增加输出爱好信息
public void display() {
super.display();
System.out.println("爱好:" + hobby);
}
// 覆盖父类的say方法,输出美式打招呼的信息
public void say() {
System.out.println("Hi, everyone! I am an American. Nice to meet you all!");
}
}
// 由Person类派生出China类,增加国籍属性,覆盖父类方法
class China extends Person {
String nationality;
// 构造函数
public China(int height, int weight, int age, String nationality) {
// 调用父类的构造函数
super(height, weight, age);
this.nationality = nationality;
}
// 覆盖父类的display方法,增加输出国籍信息
public void display() {
super.display();
System.out.println("国籍:" + nationality);
}
// 覆盖父类的say方法,输出中式打招呼的信息
public void say() {
System.out.println("大家好,我是一名中国人。很高兴认识大家!");
}
}
// 由China类派生出Guangxi类,增加语言属性,覆盖父类方法
class Guangxi extends China {
String dialect;
// 构造函数
public Guangxi(int height, int weight, int age, String nationality, String dialect) {
// 调用父类的构造函数
super(height, weight, age, nationality);
this.dialect = dialect;
}
// 覆盖父类的display方法,增加输出方言信息
public void display() {
super.display();
System.out.println("方言:" + dialect);
}
// 覆盖父类的say方法,输出关西方言打招呼的信息
public void say() {
System.out.println("嗨,同志们好啊! 我系一个桂平铁花郎,兴县塘豆腐买八两!");// 这里是广西壮话
}
}
public class Main {
public static void main(String[] args) {
// 创建一个Person对象
Person person = new Person(180, 75, 25);
System.out.print("Person的信息:");
person.display();
System.out.print("Person的招呼:");
person.say();
// 创建一个Usa对象
Usa usa = new Usa(175, 70, 30, "Basketball");
System.out.print("Usa的信息:");
usa.display();
System.out.print("Usa的招呼:");
usa.say();
// 创建一个China对象
China china = new China(170, 65, 28, "Chinese");
System.out.print("China的信息:");
china.display();
System.out.print("China的招呼:");
china.say();
// 创建一个Guangxi对象
Guangxi guangxi = new Guangxi(165, 60, 26, "Chinese", "桂林话");
System.out.print("Guangxi的信息:");
guangxi.display();
System.out.print("Guangxi的招呼:");
guangxi.say();
}
}
```
在上面的代码中,我们定义了一个`Person`类,它具有身高/体重/年龄三个属性,并实现了对这些属性进行输出和打招呼的方法。然后,我们创建了基于`Person`类的`Usa`类和`China`类,它们分别增加了爱好和国籍两个属性,并覆盖了父类的输出和打招呼方法。最后,我们还创建了基于`China`类的`Guangxi`类,它增加了方言属性,并覆盖了父类的输出和打招呼方法。在主函数中,我们创建了不同类的对象,并输出了它们的信息和打招呼语句。
相关问答
Q1: 求镂空的倒金字塔java代码....金字塔可用*代替! 谢了
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
// 点阵法:
// 首先,我们假设金字塔是等边三角形,等边三角形与矩形的关系是:
// 1, 底边是矩形的宽度
// 2, 高是举行的长度
// 3, 顶点是矩形的底边中点
// *******
// |* *|
// | * * |
// ---*---
// 这样,如果我们知道矩形的长和宽,我们就能指导等边三角形在矩形每一行中的点位于什么位置了:
// 若长(height)为4,宽(width)为7.
// 计算等边三角形在某一行x的两点位置为:
// 若x == 1, 则整行都是三角形的边
// 若x == 4, 则只有中点是三角形的边。中点 = width / 2 + (width % 2 0 ? 1 : 0)
// 若 x 1 x 4,则左点 = width / (2 * height) * x; 右点 = width - width / (2 * height) * x + 1;
// 知道了三角形的点,我们就能画出金字塔了。
// Java2D
// 首先,找到三角形的三个点,在两两相连即可。
// 示例如下:
public class Question1 {
// 矩形
class Rec{
int height;
int width;
public Rec(int height, int width){
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
}
// 以字符串打印形式绘制[点阵法]
public Question1(int height, int width, char shape) {
height = height = 0 ? 3 : height; // 对参数进行验证整理
width = width = 0 ? 3 : width; // 对参数进行验证整理
// 每行有多少点?
int rowPoints = width;
// 不要忘记每行最后还有换行符
rowPoints ++;
// 总共有多少点?
int totalPoints = rowPoints * height;
char[] allChar = new char[totalPoints]; // 所有行的字符
// 特殊处理第一行
for(int i = 0; i rowPoints; i++){
if(i rowPoints - 1) // rowpoints位是换行符,所以这里需要特殊处理。
allChar[i] = shape;
else
allChar[i] = '\n';
}
// 处理从第二行到倒数第二行
for(int i = 2; i height; i++){
//左点 = width / (2 * height) * x; 右点 = width - width / (2 * height) * x + 1;
// 但是这里得牢记,运算符的运算顺序,所以必须这样写:
int leftpoint = (width * i) / (2 * height);
int rightpoint= width - (width * i) / (2 * height) + 1;
for(int j = 0; j rowPoints; j++){
int index = (i - 1) * rowPoints + j; // 这里对数组index的计算很重要,千万别算错了。
if( j rowPoints - 1){
if(j + 1 == leftpoint || j + 1 == rightpoint) // 列序号从0开始,但点位是从1开始的,所以给列序号+1
allChar[index] = shape;
else
allChar[index] = ' ';
}else{
allChar[index] = '\n';
}
}
}
//特殊处理最后一行
int point = width / 2 + (width % 2 0 ? 1 : 0);
int startIndex = (height - 1) * rowPoints;
for(int i = 0; i rowPoints; i++){
if( i rowPoints - 1){
if( i + 1 == point)
allChar[startIndex + i] = shape;
else
allChar[startIndex + i] = ' ';
}else
allChar[allChar.length - 1] = '\n';
}
String result = new String(allChar);
System.out.print(result);
}
class PanelContainer extends JPanel{
private Point point1;
private Point point2;
private Point point3;
public PanelContainer(Point point1, Point point2, Point point3){
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
setBackground(Color.white);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(point1 != null point2 != null point3 != null){
g.setColor(Color.red);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point2.getX(), (int)point2.getY());
g2d.drawLine((int)point2.getX(), (int)point2.getY(), (int)point3.getX(), (int)point3.getY());
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point3.getX(), (int)point3.getY());
}
}
}
//Java2D
public Question1(int height, int width) {
JFrame frame = new JFrame("Java2D 三角形");
frame.setBounds(50, 50, 400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Point point1 = new Point(50, 50);// 让图形举例边界50像素
Point point2 = new Point(width + 50, 50);
Point point3 = new Point(width/2 + 50, height + 50);
PanelContainer container = new PanelContainer(point1, point2, point3);
frame.getContentPane().add(container);
frame.setVisible(true);
}
public static void main(String args[]){
new Question1(200, 300, '*');
new Question1(200, 300);
}
}
Q2: 求一个50行左右的JAVA代码,最好每行带注释,谢谢啦
/*这个相当详细了.
程序也不算太难.而且给老师看的时候效果比较好.因为有图形化界面,又实现一个比较实用的功能.老师会比较高兴的.
建立一个文件名为Change.java就可以编译了*/
/*
* 这个程序实现输入身高算出标准体重,输入体重,算出身高的功能
*/
import java.awt.*; //导入相关类包,这才样使用相应awt图形界面的类
import java.awt.event.*;//同上
public class Change extends Frame { //定义一个类Change, 父类是Frame(图形界面的)
Button b = new Button("互查"); //创建一个按钮的对象b,显示为"互查"
Label l1 = new Label("身高(cm)");//创建一个lable.显示身高
Label l2 = new Label("体重(kg)");//创建一个lable 显示体重
double heigth, weigth; //定义变量
double x, y; //定义变量
TextField tf1 = new TextField(null, 10);//添加Text框
TextField tf2 = new TextField(null, 10);//添加Text框
public Change() {//类的构造函数,完成初始化
super("互查表");//创建窗口,标题为互查表
setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局
add(l1);//把lable 身高放到window里
add(tf1);//把Text 框 放到窗口上
add(l2); //把lable 体重放到window里
add(tf2);//Test放到窗口里
add(b);//把button放到窗口上
pack();//自动放到窗口里排列上边的组件
setVisible(true);//可以让用户看到窗口
addWindowListener(new WindowAdapter() {//如果按 X, 关闭窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());//添加button监听函数
}
class ButtonListener implements ActionListener {//实现click button时功能操作
public void actionPerformed(ActionEvent e) {//当click调用
if (tf1.getText()!=null) {//检查tf1 test 是否为空
try {//取异常
x = Double.parseDouble(tf1.getText());//字符转为double型
weigth = (x - 100) * 0.9;//算重量
tf2.setText("" + weigth);//显示重量
} catch (NumberFormatException ex) {
tf1.setText("");//如果输入不是数字,设为空
}
}
if (tf1.getText().equals("")==true){//tf1是否为空
y = Double.parseDouble(tf2.getText());//把tf2里的文本转为double 型 的
heigth = y / 0.9 + 100; //算身高根据重量
tf1.setText("" + heigth);}//显示身高
}
}
public static void main(String[] args) {//主函数,程序入口
new Change(); //建立类Change的对象,并调用他的构造函数Change().显示窗口
}
}
Q3: java代码示例
importjava.awt.*;importjava.awt.event.*;classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面纸:3元",Label.LEFT);label2=newLabel("钢笔:5元",Label.LEFT);label3=newLabel("书:10元",Label.LEFT);label4=newLabel("袜子:8元",Label.LEFT);button1=newButton("加入购物车");button2=newButton("加入购物车");button3=newButton("加入购物车");button4=newButton("加入购物车");button5=newButton("查看购物车");text=newTextArea("商品有:"+"\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一个面纸、");sum=sum+3;}elseif(e.getSource()==button2){text.append("一只钢笔、");sum=sum+5;}elseif(e.getSource()==button3){text.append("一本书、");sum=sum+10;}elseif(e.getSource()==button4){text.append("一双袜子、");sum=sum+8;}elseif(e.getSource()==button5){text.append("\n"+"总价为:"+"\n"+sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("购物车");}}我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。
java精美代码示例的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 代码简洁、java精美代码示例的信息别忘了在本站进行查找喔。







