public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world" );
}
}
class FreshJuice{
enum FreshJuiceSize{SMALL,MEDIUM,LARGE};
FreshJuiceSize size;
}
public class FreshJuiceTest{
public static void main(String[] args){
FreshJuice juice= new FreshJuice();
juice.size =FreshJuice.FreshJuiceSize.MEDIUM;
}
}
public class Puppy {
//2.1首先定义类的构造函数
public Puppy(String name){
System.out.println("我的小狗的名字是:"+name);
}
//2.2主函数创建对象,并用构造函数初始化对象
public static void main(String[] args) {
Puppy myPuppy=new Puppy("欢欢");
}
}
public class Cat {
int catAge;//实例变量
public Cat(String name){//构造方法
System.out.println("我的小猫的名字是:"+name);
}
public void setAge (int age){//成员方法
catAge=age;
}
public int getAge(){
System.out.println("我的小猫的年龄是:"+catAge);//成员方法调用实例变量
return catAge;
}
public static void main(String[] args) {
Cat mycat=new Cat("费力罗");//使用构造方法初始化了一个对象
mycat.setAge(3);//使用成员方法,设定实例变量
mycat.getAge();
System.out.println("年龄:"+mycat.catAge);//访问成员变量
}
}
Employee.java
public class Employee {
String name;
int age;
String designation;
Double salary;
public Employee(String name){
this.name =name;
}
public void empAge(int empAge){
age=empAge;
}
public void empDesignation(String empDesignation){
designation=empDesignation;
}
public void empSalary(double empSalary){
salary =empSalary;
}
public void printEmployee(){
System.out.println("名字:"+name);
System.out.println("年龄:"+age);
System.out.println("职位:"+designation);
System.out.println("薪水:"+salary);
}
}
EmployeeTest.java,程序都是从main方法开始执行
import java.io.*;//载入java_installation/java/io路径下的所有类
public class EmployeeTest {
public static void main(String[] args) {
Employee empOne= new Employee("张三");
Employee empTwo= new Employee("李四");
empOne.empAge(26);
empOne.empDesignation("高级工程师");
empOne.empSalary(10000);
empOne.printEmployee();
empTwo.empAge(62);
empTwo.empDesignation("初级工程师");
empTwo.empSalary(100);
empTwo.printEmployee();
}
}
final关键字修饰常量;前缀0表示8进制,0x表示16进制
参数变量的两种传递方式:值传递和引用传递
public class RunoobTest {
private int instanceVar;//成员变量
private static int staticVar;//静态变量
public void method(int paramVar){//参数变量
int localVar=10;//局部变量
instanceVar=localVar;
staticVar=paramVar;
System.out.println("成员变量:"+instanceVar);
System.out.println("静态变量:"+staticVar);
System.out.println("参数变量:"+paramVar);
System.out.println("局部变量:"+localVar);
}
public static void main(String[] args) {
RunoobTest v=new RunoobTest();
v.method(20);
}
}
6.1Extends继承一个父类,implements继承多个接口
public class Animal {
private String name;
private int id;
public Animal(String myname,int myid){
name=myname;
id=myid;
}
public void eat(){
System.out.println(name+"正在吃");
}
public void sleep(){
System.out.println(name+"正在睡");
}
public void introduction(){
System.out.println(name+"大家好!我是"+id+"号"+name+"。");
}
}
public class Penguin extends Animal{
public Penguin(String myname,int myid){
super(myname,myid);
}
}
public class Mouse extends Animal{
public Mouse(String myname,int myid){
super(myname,myid);
}
}
6.2super关键字实现对父类成员的访问;this关键字指向自己的引用
class Creature {
void eat(){
System.out.println("creature:eat");
}
}
class Dog extends Creature{
void eat(){
System.out.println("dog:eat");
}
void eatTest(){
this.eat();//this调用自己的方法
super.eat();//调用父类的方法
}
}
public class Test{
public static void main(String[] args) {
Creature a=new Creature();
a.eat();//父类对象用方法
Dog d=new Dog();
d.eatTest();//子类对象用两种方法
}
}
abstract class Animal2{
abstract void eat();
}
class Cat2 extends Animal2{
public void eat(){
System.out.println("吃鱼");
}
public void work(){
System.out.println("抓老鼠");
}
}
class Dog2 extends Animal2{
public void eat(){
System.out.println("吃骨头");
}
public void work(){
System.out.println("看家");
}
}
public class Test2 {
public static void show(Animal2 a){
a.eat();
if (a instanceof Cat2){
Cat2 c=(Cat2)a;
c.work();
}else if (a instanceof Dog2){
Dog2 c=(Dog2)a;
c.work();
}
}
public static void main(String[] args) {
show(new Cat2());//Cat对象调用show方法
show(new Dog2());
Animal2 a=new Cat2();//向上转型
a.eat();
Cat2 c=(Cat2)a;//向下转型
c.work();
}
}
/* EncapTest.Java*/
public class EncapTest {
private String name;
public void setName(String newName){
name=newName;
}
public String getName(){
return name;
}
private String idNum;
public void setIdNum(String newId){
idNum=newId;
}
public String getIdNum(){
return idNum;
}
private int age;
public void setAge(int newAge){
age=newAge;
}
public int getAge(){
return age;
}
}
/*RunEncap.java*/
public class RunEncap {
public static void main(String[] args) {
EncapTest encap=new EncapTest();
encap.setName("zoe");
encap.setAge(28);
encap.setIdNum("757");
System.out.println("Name:"+encap.getName()+"age:"+encap.getAge() );
}
}
enum Color{
RED,GREEN,BLUE;
private Color(){//构造函数
System.out.println(this.toString()+"构造时会遍历");
}
public void colorInfo(){
System.out.println("color");
}
}
public class Test3
{
public static void main(String[] args) {
Color c=Color.RED;
System.out.println(c);
c.colorInfo();
}
}