📄 f.txt
字号:
-------------------------1---------------
class Count {
private int serial ;
private static int counter = 0 ;
Count() {
counter++;
serial = counter ;
}
int getserial(){ return serial ;}
}
class StaticVar {
static int x = 100 ;
}
public class test {
public static void main(String[] args){
Count c1 = new Count();
Count c2 = new Count();
System.out.println(c1.getserial());
System.out.println(c2.getserial());
System.out.println(StaticVar.x++);
System.out.println(StaticVar.x++);
System.out.println("E= " + Math.E);
System.out.println("PI= " + Math.PI);
}
}
-------------------------2------------------------
public class e43
{
public static void main(String args[])
{
PhoneCard200 my200_1 = new PhoneCard200();
PhoneCard200 my200_2 = new PhoneCard200();
PhoneCard200.additoryFee = 0.1;
System.out.println("第1张200卡的附加费:"+my200_1.additoryFee);
System.out.println("第2张200卡的附加费:"+my200_2.additoryFee);
System.out.println("200卡类的附加费:"+PhoneCard200.additoryFee);
System.out.println("200卡接入号码:" + PhoneCard200.connectNumber);
System.out.println("第1张200卡的接入号码:" + my200_1.connectNumber);
}
}
class PhoneCard200
{
static final String connectNumber = "200";
static double additoryFee;
long cardNumber ;
int password;
boolean connected;
double balance;
}
---------------------3----------------------
public class a {
public static void main(String[] args){
System.out.println(Math.round(3.54));
String s = to_char(2.718);
System.out.println("e=" + s);
}
static String to_char(double x){
return Double.toString(x);
}
}
--------------------4--------------------
public class e44
{
public static void main(String args[])
{
PhoneCard200 my200_1 = new PhoneCard200();
PhoneCard200 my200_2 = new PhoneCard200();
System.out.println("第一张200卡的卡号:"+my200_1.cardNumber);
System.out.println("第二张200卡的卡号:"+my200_2.cardNumber);
}
}
class PhoneCard200
{
static long nextCardNumber;
static String connectNumber = "200";
static double additoryFee;
long cardNumber ;
int password;
boolean connected;
double balance;
static
{
nextCardNumber = 2001800001;
}
PhoneCard200()
{
cardNumber = nextCardNumber++;
}
}
--------------------5----------------------
//建立包,加入类
package mypk ;
public class A{
public void print(String s){
B b1 = new B();
System.out.println(b1.addString(s) );
}
}
class B{
String addString(String s){
return "-----" + s + "-----" ;
}
}
//javac -d d:\ A.java 将 mypk包 建在d:\目录下
//测试程序
import mypk.* ;
public class test{
public static void main(String[] args){
A a1 = new A();
a1.print("test");
}
}
//将d:\目录追加到classpath环境变量中
// javac test.java
// java test
---------------------6---------------------
package mypk ;
public class pubclass {
private String a1="private";
String a2="package";
protected String a3="protected";
public String a4="public";
private String get1(){
return "privateMethod " + a1 ;
}
String get2(){
return "packageMethod " + a1 ;
}
protected String get3(){
return "protectedMethod " + a1;
}
public String get4(){
return "publicMethod " + a1 ;
}
}
class nopubclass
{ public int x=1;
public int getx(){
return x ;
}
}
//-------
//测试访问其他包中的类
import mypk.* ;
class test1 {
public static void main(String args[]) {
pubclass pc =new pubclass(); // 建立mypk包的public类对象
System.out.println(pc.a1+pc.a2+pc.a3+pc.a4);
System.out.println(pc.a4);
System.out.println(pc.get1()+pc.get2()+pc.get3()+pc.get4());
System.out.println(pc.get4());
nopubclass nopubc=new nopubclass(); // 建立mypk包的非public类对象
}
}
//-----------测试子类继承其他包中的-------------
import mypk.* ;
class test2
{
public static void main(String args[])
{
subClass sc= new subClass();
System.out.println(sc.geta());
System.out.println(sc.a3 + sc.a4);
}
}
class subClass extends pubclass {
String geta() {
return a1+a2+a3+a4 ;
}
}
--------------------7------------------
interface CalArea {
double pi = 3.14 ;
double cal(double r);
}
class a implements CalArea {
double cal(double r) { //
return pi * r *r ;
}
}
public class test {
public static void main(String[] args){
a a1 = new a() ;
System.out.println(a1.cal(5.0));
System.out.println(a1 instanceof CalArea );
}
}
------------- parcel2.java 在类中定义内层类-------
public class Parcel2 {
class Contents { //inner class 1
private int i ;
Contents(int cn){ i=cn;}
public int value() { return i; }
}
class Destination { //inner class 2
private String label;
Destination(String whereTo) {label = whereTo; }
String readLabel() { return label; }
}
public Destination to(String s) { //method 1
return new Destination(s);
}
public Contents cont(int cn) { //method 2
return new Contents(cn);
}
public void ship(String dest,int cn) { //method 3
Contents c = cont(cn);
Destination d = to(dest);
System.out.println("Contents: " + c.value());
System.out.println("Distination: " + d.readLabel());
}
}
class pl2 {
public static void main(String[] args) {
Parcel2 p1 = new Parcel2();
p1.ship("Tanzania",100);
Parcel2.Contents cc = p1.new Contents(1010);
System.out.println(cc.value());
Parcel2 p2 = new Parcel2();
Parcel2.Contents c = p2.cont(200); // return handles to inner classes
Parcel2.Destination d = p2.to("Borneo");
System.out.println(c.value());
System.out.println(d.readLabel());
}
}
-----parcel3.java 内层类实现接口和基类的抽象方法-------
abstract class Contents { //不一定是抽象类
abstract public int value();
}
interface Destination { //典型应用
String readLabel();
}
public class Parcel3 {
private class PContents extends Contents {
private int i ;
PContents(int cn){i=cn;}
public int value() { return i; } //method overriding
}
protected class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; } //implement method
}
public Destination dest(String s) {
return new PDestination(s);
}
public Contents cont(int cn) {
return new PContents(cn);
}
}
class pl3 {
public static void main(String[] args) {
Parcel3 p = new Parcel3();
Contents c = p.cont(100); //upcast
Destination d = p.dest("Tanzania"); //upcast
System.out.println(c.value());
System.out.println(d.readLabel());
// Illegal -- can't access private class:
//! Parcel3.PContents c = p.new PContents();
}
}
---------parcel4.java 在方法中定义内层类------------
interface Destination {
String readLabel();
}
public class Parcel4 { // Parcel4类
public Destination dest(String s) { // dest方法
class PDestination
implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; } //重载
}
return new PDestination(s);
}
}
class pl4 {
public static void main(String[] args) {
Parcel4 p = new Parcel4();
Destination d = p.dest("Tanzania"); // 提供处理句柄
System.out.println(d.readLabel());
}
}
----- parcel5.java 在语句(条件语句分支)中定义内层类-----
public class Parcel5 {
void internalTracking(boolean b) {
if(b) {
class TrackingSlip {
private String id;
TrackingSlip(String s) {
id = s;
}
String getSlip() { return id; }
}
TrackingSlip ts = new TrackingSlip("slip");
String s = ts.getSlip();
System.out.println(s);
}
// Can't use it here! Out of scope:
//! TrackingSlip ts = new TrackingSlip("x");
}
}
class pl5 {
public static void main(String[] args) {
Parcel5 p = new Parcel5();
p.internalTracking(true);
}
}
//在条件语句中的内部类 TrackingSlip并不表示有条件的建立
//同其他类一样,在编译时就一道解决了。
//内部类只是限定它的作用域(在条件分支中使用)。
-------- parcel6.java 实现接口的内层匿名类 --------
//-------无名内层类--------------
interface Contents {
int value();
}
public class Parcel6 {
public Contents cont() {
return new Contents() {
private int i = 11;
public int value() { return i; }
}; // 此处要冒号
}
}
class pl6 {
public static void main(String[] args) {
Parcel6 p = new Parcel6();
Contents c = p.cont();
System.out.println(c.value());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -