- 浏览: 31036 次
- 性别:
- 来自: 深圳
-
最新评论
文章列表
Object类之toString()方法
TestToString.java
//Object类之toString()方法
public class TestToString {
public static void main(String[] args) {
Dog d = new Dog();
System.out.println("d:=" + d);
}
}
class Dog {
//重写Object中的toString()方法
public String toString() {
return "I' ...
继承中的构造方法——super关键字
TestSuperSub.java
//继承中的构造方法——super关键字
class SuperClass {
private int n;
SuperClass() {
System.out.println("父类无参构造方法SuperClass()");
}
SuperClass(int n) {
...
super关键字的使用
TestInherit.java
//super关键字
class FatherClass {
public int value;
public void f(){
value = 100;
System.out.println
("FatherClass.value="+value);
}
}
class ChildClass extends FatherClass {
public int value;
public void f ...
java中的重写override或overwrite
TestOverWrite.java
class Person {
private String name;
private int age;
public void setName(String name){this.name=name;}
public void setAge(int age) {this.age=age;}
public String getName(){return name;}
public int getAge(){return age;}
...
类的继承
TestPerson.java
class Person {
private String name;
private int age;
public void setName(String name) {
this.name=name;
}
public void setAge(int age) {
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
...
java中的jar命令
F:\>cd java
F:\java>jar
用法: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
选项:
-c 创建新档案
-t 列出档案目录
-x 从档案中提取指定的 (或所有) 文件
-u 更新现有档案
-v 在标准输出中生成详细输出
-f 指定档案文件名
-m 包含指定清单文件中的清单信息
-n 创建新档案后执行 Pack2 ...
测试package和import语句
Cat.java
package com.bjsxt.java140;//package避免类名重复无法识别
public class Cat {
}
Dog.java
package com.bjsxt.java139;
import com.bjsxt.java140.*;//imoprt语句
public class Dog {
public static void main(String[] args) {
com.bjsxt.java140.Cat c = new com.bjsxt.java140 ...
static关键字
用static声明的变量为静态成员变量,通过引用或类名访问静态成员,不需要实例化。
//staic关键字
//非静态的成员变量不能在静态的上下文中使用
public class Cat {
private static int sid = 0;
private String name;
int id;
Cat(String name) {
this.name = name;
id = sid++;
}
public void info(){
Sy ...
定义一个圆,求圆的面积
TestCircle .java
class Point {
private double x;
private double y;
Point(double x1, double y1) {
x = x1;
y = y1;
}
public double getX() { return x; }
public double getY() { return y; }
public void setX(double i) { x = i; }
public v ...
方法的重载
TestOverLoad.java
//方法的重载
//方法名相同,返回类型相同,方法参数不同
public class TestOverload {
void max(int a , int b) {
System.out.println( a > b ? a : b );
}
void max(short a , short b) {
System.out.println("short");
System.out.println( a > b ? a : b );
}
void max( ...
定义一个点Ponit类,用来表示三维空间中的点
要求如下:
1.可以生成特定坐标的点对象
2.提供设置坐标的方法
3.计算该点与原点距离的平方的方法
TestPoint.java
/*
定义一个点Ponit,用来表示三维空间中的点,要求如下:
1.可以生成特定坐标的点对象
2.提供设置坐标的方法
3.计算该点与原点距离的平方的方法
*/
class Point {
double x, y, z;
//构造方法,生成特定的点对象
Point(double x, double y, double z){
this.x = x;
this.y ...
1.通过递归计算5的阶乘
TestDiGui.java
//通过递归计算5的阶乘
public class TestDiGui {
public static void main(String[] args) {
System.out.println(m(5)); //结果为120
}
public static int m(int n) {
if(n == 1) return 1;
else return n*m(n-1);
}
}
方法调用图:
2.通过递归方法计算fibnacci数列
TestFib.java
//通过递归计 ...
使用while和do while循环完成0到9的输出
whileDoWhile.java
//使用while和do while循环完成0到9的输出
public class WhileDoWhile {
public static void main(String[] args) {
int i = 0;
while(i<10) {
System.out.print(i +",");
i++;
}
System.out.println();
int j = 0;
do {
System.o ...
两java小程序,熟练使用for循环
1.计算1~10的阶乘的和
2.计算100以内奇数的和
1.计算1~10的阶乘的和
Jiecheng.java
//计算result = 1! + 2! + ... + 10!
public class Jiecheng {
public static void main(String[] args) {
int jie = 1;
int result = 0;
for(int i=1; i <= 10; i++) {
jie = jie * i;
result += jie;
} ...