import java.util.*; abstract class Figure
{
protected String shape; protected Figure(String shape)
{
this.shape = shape;
} protected Figure()
{
this("未知");
} public abstract double area(); public abstract double perimeter(); public void print()
{
System.out.println("一个" + this.shape + "," + this.toString() + ",周长为"
+ this.perimeter() + ",面积为" + this.area());
}
}class Triangle extends Figure
{
protected double a;
protected double b;
protected double c; public Triangle(double a, double b, double c)
{
super("三角形");
this.a = a;
this.b = b;
this.c = c;
} public Triangle()
{
this(0, 0, 0);
} public String toString()
{
return "a边长" + this.a + ",b边长" + this.b + ",c边长" + this.c;
} public double area()
{
return (Math.sqrt((a + b + c) / 2.0) * (((a + b + c) / 2.0) - a)
* (((a + b + c) / 2.0) - b) * (((a + b + c) / 2.0) - c));
} public double perimeter()
{
return (a + b + c);
}
}到这里是正确的,编译通过,但是确实主函数
不知道以下抽象类的对象引用子类实例是否正确class ClosedFigure_ex
{
public static void main(String args[])
{ClosedFigure g=new Triangle(3,4,5);
g.print();
}
}
{
protected String shape; protected Figure(String shape)
{
this.shape = shape;
} protected Figure()
{
this("未知");
} public abstract double area(); public abstract double perimeter(); public void print()
{
System.out.println("一个" + this.shape + "," + this.toString() + ",周长为"
+ this.perimeter() + ",面积为" + this.area());
}
}class Triangle extends Figure
{
protected double a;
protected double b;
protected double c; public Triangle(double a, double b, double c)
{
super("三角形");
this.a = a;
this.b = b;
this.c = c;
} public Triangle()
{
this(0, 0, 0);
} public String toString()
{
return "a边长" + this.a + ",b边长" + this.b + ",c边长" + this.c;
} public double area()
{
return (Math.sqrt((a + b + c) / 2.0) * (((a + b + c) / 2.0) - a)
* (((a + b + c) / 2.0) - b) * (((a + b + c) / 2.0) - c));
} public double perimeter()
{
return (a + b + c);
}
}到这里是正确的,编译通过,但是确实主函数
不知道以下抽象类的对象引用子类实例是否正确class ClosedFigure_ex
{
public static void main(String args[])
{ClosedFigure g=new Triangle(3,4,5);
g.print();
}
}
Figure g = new Triangle(3, 4, 5);
//如何生成可以双击的jar文件?
1.c盘下建立一个文件,
文件名:manifest.mf
文件内容:Main-Class: test.HelloWorld2.c盘下建立一个文件夹,
文件夹名:test
3.在c盘下的子文件夹test中建立一个文件
文件名:HelloWorld.java
文件内容:package test;
import javax.swing.*;
class HelloWorldMainFrame extends JFrame {
public HelloWorldMainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200, 200, 30, 50);
add(new JLabel("HelloWorld"));//显示
setVisible(true);
}//还可以有内部类,匿名内部类......
}
//如果有多个类文件,或者同一个文件有多个类,编译之后会产生多个类文件。我把它写成两个类。最终会产生两个.class文件
public class HelloWorld {//无论如何要记住这个类名,因为它是程序的入口
public static void main(String[] args) {
new HelloWorldMainFrame();
}
}4.c盘下建立一个文件,
文件名:HelloWorld.bat
文件内容:javac test/HelloWorld.java
javac test/*.java
jar cvfm test.jar manifest.mf test
java -jar test.jar
echo 先双击HelloWorld.bat文件 以后直接双击c盘下的test.jar
exit