Java
Java
1Class
public class Car { ... }
2Object
Car myCar = new Car();
3Inheritance
public class Dog extends Animal { ... }
4Encapsulation
private String name; public String getName() { return name; }
5Polymorphism
public int add(int a, int b) { ... }public double add(double a, double b) { ... }@Override public void makeSound() { System.out.println("Meow"); }
6Abstraction
public abstract class Shape { abstract void draw(); }public interface Animal { void eat(); }
7Interface
public interface Drivable { void drive(); }
8Method
public void displayInfo() { System.out.println("Info"); }
9Method Overloading
-
public class MathUtils { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }
boygirlclassobject
classobject
Java
Java
Java
Java
public class Dog {
String breed;
int size;
String colour;
int age;
void eat() {
}
void run() {
}
void sleep(){
}
void name(){
}
}
- static
eat()run()sleep() name() Dog
Java
public class Puppy{
public Puppy(){
}
public Puppy(String name){
// name
}
}
Java new
- new
- new
public class Puppy{
public Puppy(String name){
//name
System.out.println(" : " + name );
}
public static void main(String[] args){
// Puppy
Puppy myPuppy = new Puppy( "tommy" );
}
}
: tommy
/* */
Object referenceVariable = new Constructor();
/* */
referenceVariable.variableName;
/* */
referenceVariable.methodName();
Object Object
Puppy.java
public class Puppy {
private int age;
private String name;
//
public Puppy(String name) {
this.name = name;
System.out.println(" : " + name);
}
// age
public void setAge(int age) {
this.age = age;
}
// age
public int getAge() {
return age;
}
// name
public String getName() {
return name;
}
//
public static void main(String[] args) {
//
Puppy myPuppy = new Puppy("Tommy");
// age
myPuppy.setAge(2);
// age
int age = myPuppy.getAge();
System.out.println(" : " + age);
// getter
System.out.println(" : " + myPuppy.getAge());
}
}
: tommy : 2 : 2
import package
- public
- public
- public public EmployeeEmployee.java
- package
- import package package import
- import package
final
Java
Java
import
Java Java import
java_installation/java/io
import java.io.*;
Employee EmployeeTest
Employee.java
Employee nameagedesignation salary
Employee.java
import java.io.*;
public class Employee {
private String name;
private int age;
private String designation;
private double salary;
// Employee
public Employee(String name) {
this.name = name;
}
// age
public void setAge(int age) {
this.age = age;
}
// age
public int getAge() {
return age;
}
// designation
public void setDesignation(String designation) {
this.designation = designation;
}
// designation
public String getDesignation() {
return designation;
}
// salary
public void setSalary(double salary) {
this.salary = salary;
}
// salary
public double getSalary() {
return salary;
}
//
public void printEmployee() {
System.out.println(this);
}
// toString
@Override
public String toString() {
return ": " + name + "\n" +
": " + age + "\n" +
": " + designation + "\n" +
": " + salary;
}
}
Java main main
EmployeeTest 2 Employee
EmployeeTest.java
EmployeeTest.java
import java.io.*;
public class EmployeeTest {
public static void main(String[] args) {
//
Employee empOne = new Employee("RUNOOB1");
Employee empTwo = new Employee("RUNOOB2");
//
empOne.setAge(26);
empOne.setDesignation("");
empOne.setSalary(1000);
empOne.printEmployee();
empTwo.setAge(21);
empTwo.setDesignation("");
empTwo.setSalary(500);
empTwo.printEmployee();
}
}
EmployeeTest
$ javac EmployeeTest.java Employee.java $ java EmployeeTest :RUNOOB1 :26 : :1000.0 :RUNOOB2 :21 : :500.0
stinkaroo
190***[email protected]
javapublic
stinkaroo
190***[email protected]
LadyLeane
q-b***sn.com
package c++ namespace Java package class package aaa.bbb.ccc .java ./aaa/bbb/ccc/
import package ./aaa/bbb/ccc/ A importAnew aaa.bbb.ccc.A() import aaa.bbb.ccc.A new A() aaa.bbb.ccc.
LadyLeane
q-b***sn.com
253***[email protected]
JAVAPublic?
java public main (main) C main() public public
() public public public public() public
253***[email protected]
pxn626
790***[email protected]
static
1
2
3
4
static ()
1
2.
3
1
2
3thissuper
thisthis
1
2
1
2
3
4
pxn626
790***[email protected]
Demetris
150***[email protected]
1.
public class ClassName{ public void printNumber{ int a; } // }2.
public class ClassName{ int a; public void printNumber{ // } }3. static
public class ClassName{ static int a; public void printNumber{ // } }Demetris
150***[email protected]
ycxchkj
xch***163.com
1
2
3new
4JavaJava
new
1
2
3
ycxchkj
xch***163.com
Lynn
276***[email protected]
Oracle
Lynn
276***[email protected]
lllunaticer
tdl***tju.edu.cn
java
// Public class People{ int age = 23; Public void getAge(){ System.out.print("the age is "+age); } } // People xiaoMing = new People(); // People() People xiaoMing.getAge();//// Public class People{ int age = 23; Public void getAge(){ System.out.print("the age is "+ age); } // Public People(int a){ this.age = a; } } // People xiaoMing = new People(20); // xiaoMing.getAge(); // 20lllunaticer
tdl***tju.edu.cn
2333
135***[email protected]
1.
2.
3., this.color
this
new Student(); new
2333
135***[email protected]
tfbyly
905***[email protected]
1
public class Person { private int blood; private Heart heart; } public class Heart { private int blood; public void test() { System.out.println(blood); } }2
public class Person { private int blood; public class Heart { public void test() { System.out.println(blood); } } public class Brain { public void test() { System.out.println(blood); } } }// class Out { private int age = 12; // class In { public void print() { System.out.println(age); } } } public class Demo { public static void main(String[] args) { Out.In in = new Out().new In(); in.print(); // /* Out out = new Out(); Out.In in = out.new In(); in.print(); */ } }12
.class Out.class Out$In.class
$ Out.In
Out.In in = new Out().new In()
2
java
Javatfbyly
905***[email protected]
hua***[email protected]
Java this super
class Person { public static void prt(String s) { System.out.println(s); } Person() { prt("· "+"A Person."); }//(1) Person(String name) { prt("· "+"A person's name is " + name); }//(2) } public class Chinese extends Person { Chinese() { super(); // 1 prt("·”“ "+"A chinese coder."); } Chinese(String name) { super(name);// 2 prt("·”“ "+"his name is " + name); } Chinese(String name, int age) { this(name);// 3 prt("his age is " + age); } public static void main(String[] args) { Chinese cn = new Chinese(); cn = new Chinese("codersai"); cn = new Chinese("codersai", 18); } }hua***[email protected]
samcyang
532***[email protected]
JAVA C++
// Public class People{ int age = 23; Public void getAge(){ System.out.print("the age is "+ age); } // Public People(int a){ this.age = a; } } // People xiaoMing = new People(20); // People xiaoMing2 = new People(); // ERROR xiaoMing.getAge(); // 20samcyang
532***[email protected]
Phanio
pen***[email protected]
CMD Java GBK
javac -encoding UTF-8 .java
JDK -encoding JAVA java.exe JAVA JDK file.encoding win2k GBK JDK JAVA file.encoding JAVA UNICODE javac UNICODE class .class UNICODE JDK UNICODE .class .class : javac -encoding gbk xx.java
Phanio
pen***[email protected]
Ekko404
283***[email protected]
class A{} //eat()run() sleep() Dog
new :
:
public (){} public (String name){}1.
public class ClassName{ public void printNumber{ int a; } // }2.
public class ClassName{ int a; public void printNumber{ // } }3. static
public class ClassName{ static int a; public void printNumber{ // } }Ekko404
283***[email protected]
jidaojiuyou
232***[email protected]
LOL
lol
public class Hero { String name; // int attackDamage; // int abilityPower; // int armor; // int magicResistance; // float attackSpeed; // int cooldownReduction; // int criticalStrike; // int moveSpeed; // int hp; // int mp; // }K
public class Hero { public void DestroyTower(){ System.out.println(""); } public void Keng(){ System.out.println(""); } public void Kb(){ System.out.println(""); } public void Dance(){ System.out.println(""); } }(garen) main new
public static void main(String[] args) { Hero garen = new Hero(); garen.name = ""; garen.attackDamage = 71; garen.abilityPower = 0; garen.armor = 36; garen.magicResistance = 32; garen.attackSpeed = 0.69f; garen.cooldownReduction = 0; garen.criticalStrike = 0; garen.moveSpeed = 350; garen.hp = 600; garen.mp = 0; }jidaojiuyou
232***[email protected]