老板要求的Java和PHP类比学习
Java编程语言是一种高级语言,可以通过以下所有流行语来表征,同时相比PHP,使用❎⛔️✅:
- Simple【简单】❎
- Object oriented【面向对象】⛔️
- Distributed 【分布式】✅
- Multithreaded【多线程】✅
- Dynamic【动态】❎
- Architecture neutral【架构中立性】✅
- Portable【可移植】⛔️
- High performance【高性能】✅
- Robust【强壮】✅
- Secure【安全】✅
java编译过程:.java
->.class
->01100
,编译器是JavaVM
php编译过程:.php
->opcode
->01100
,编译器是ZendVM
// java
brew install java
// php
brew install php
// java maven
brew install maven
// php composer
brew install composer
// HelloWorldApp.java
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
javac HelloWorldApp.java
java HelloWorldApp
// HelloWorldApp.php
<?php
echo "Hello World!";
php HelloWorldApp.php
楼主说:php的面向对象与java的面向对象基本一致,原因是php借鉴java
对象object
:类似现实世界的对象,他们都拥有状态
和关系行为
。一个对象存储他们的状态在一个变量
和暴露他们的行为通过方法
封装encapsulation
:隐藏状态并通过方法进行交互
类class
:我们经常会讲到new一个对象,解读为对象是类的实例,类是对象的蓝图
继承inherit
:继承来自父类的状态和方法
接口interface
:制定一套规范方法给类使用
包package
:命名空间,类似电脑中的文件夹
楼主说:java和php基本一致,只是java是强类型语言,需要定义变量的类型
- 实例变量⛔️
- 静态变量✅
- 常量⛔️
- 参数⛔️
// java
float pi = 3.14;
// php
$pi = 3.14;
- byte
- short
- int
- long
- float
- double
- boolean
- char
// java
int[] arr = {1,2,3};
int[][] array = {
{1,2,3},
{4,5,6},
{7,8,9}
};
// php
$arr = [1,2,3];
$array = [
[1,2,3],
[4,5,6],
[7,8,9]
];
###2、运算符
java和php⛔️
###3、表达式
java和php⛔️
java和php⛔️
public class Bicycle {
// 预定义变量
public int cadence;
public int gear;
public int speed;
// 构造函数
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// 设置踏板
public void setCadence(int newValue) {
cadence = newValue;
}
// 设置齿轮
public void setGear(int newValue) {
gear = newValue;
}
// 减速
public void applyBrake(int decrement) {
speed -= decrement;
}
// 加速
public void speedUp(int increment) {
speed += increment;
}
}
public class MountainBike extends Bicycle {
public int seatHeight;
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear){
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
<?php
class Bicycle
{
// 预定义变量
public $cadence;
public $speed;
public $gear;
// 构造函数
public function __construct(int $startCadence, int $startSpeed, int $startGear)
{
$this->cadence = $startCadence;
$this->speed = $startSpeed;
$this->gear = $startGear;
}
// 设置踏板
public function setCadence(int $newValue)
{
$this->cadence = $newValue;
}
// 设置齿轮
public function setGear(int $newValue)
{
$this->gear = $newValue;
}
// 减速
public function applyBrake(int $decrement)
{
$this->speed -= $decrement;
}
// 加速
public function speedUp(int $increment)
{
$this->speed += $increment;
}
}
<?php
class MountainBike extends Bicycle
{
public $seatHeight;
public function __construct(int $startHeight, int $startCadence, int $startSpeed, int $startGear)
{
parent::__construct($startCadence, $startSpeed, $startGear);
$this->seatHeight = $startHeight;
}
public function setHeight(int $newValue)
{
$this->seatHeight = $newValue;
}
}
public static void main(String[] args) {
// 实例化对象
Bicycle myBike = new Bicycle(30, 0, 8);
System.out.println(myBike.cadence+" "+myBike.gear+" "+myBike.speed);
}
// 实例化对象
$bicycle = new Bicycle(30, 0, 8);
echo $bicycle->cadence." ".$bicycle->speed." ".$bicycle->gear."\n";
✅
// java可以有多个构造函数,根据参数不同而生成的对象不同
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
✅
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
x = 23
this.x = 1
ShadowTest.this.x = 0
✅
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
✅
public class Generation3List extends Generation2List {
// Author: John Doe
// Date: 3/17/2002
// Current revision: 6
// Last modified: 4/12/2004
// By: Jane Doe
// Reviewers: Alice, Bill, Cindy
// class code goes here
}
@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
// Note use of array
String[] reviewers();
}
@ClassPreamble (
author = "John Doe",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Jane Doe",
// Note array notation
reviewers = {"Alice", "Bob", "Cindy"}
)
public class Generation3List extends Generation2List {
// class code goes here
}
- @Deprecated
- @Override
- @SuppressWarnings
- @SafeVarargs
- @FunctionalInterface
- @Retention
- @Documented
- @Target
- @Inherited
- @Repeatable
✅
// java
// 接口继承接口
public interface GroupedInterface extends Interface1, Interface2, Interface3 {
// code
}
// 实现接口
public class RectanglePlus implements Relatable {
// code
}
// php
interface GroupedInterface
{
// code
}
class RectanglePlus implements Relatable
{
// code
}
⛔️
// java
public class Bicycle {
// code
}
public class MountainBike extends Bicycle {
// code
}
public class Pegasus extends Horse implements Flyer, Mythical {
// code
}
// php
class Bicycle {
// code
}
class MountainBike extends Bicycle {
// code
}
class Pegasus extends Horse implements Flyer, Mythical {
// code
}
✅
public class MountainBike extends Bicycle {
// 通过重载实现多态
public void printDescription() {
super.printDescription();
System.out.println("The " + "MountainBike has a" +
getSuspension() + " suspension.");
}
}
✅
-
protected Object clone() throws CloneNotSupportedException
-
public boolean equals(Object obj)
-
protected void finalize() throws Throwable
-
public int hashCode()
-
public String toString()
⛔️
public abstract class GraphicObject {
abstract void draw();
}
abstract class GraphicObject {
abstract function draw();
}
Java是纯面向对象语言,所有都是类,一些数字和字符串操作都得使用类,而PHP中是函数❎
Class Number
Class Character
Class String
todo
✅
// 普通写法 需要强制转换
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0);
// 泛型写法 不需要强制转换
List<String> list = new ArrayList<String>();
list.add("hello");
String s = list.get(0);
通常使用的类型变量名字:
- E - Element (used extensively by the Java Collections Framework)
- K - Key
- N - Number
- T - Type
- V - Value
- S,U,V etc. - 2nd, 3rd, 4th types
⛔️
public class ListOfNumbers {
private List<Integer> list;
public static final int SIZE = 10;
public ListOfNumbers() {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(new Integer(i));
}
}
public void writeList(){
PrintWriter out = null;
try {
System.out.println("Entered try statement");
out = new PrintWriter(new FileWriter("src/HelloWorldApp/OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch (IndexOutOfBoundsException e) {
System.err.println("IndexOutOfBoundsException: " + e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
}
public static void main(String[] args) {
ListOfNumbers numbers = new ListOfNumbers();
numbers.writeList();
}
}
⛔️
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("src/HelloWorldApp/xanadu.txt");
out = new FileOutputStream("src/HelloWorldApp/outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
✅
// 方法1
public class HelloRunnable implements Runnable {
@Override
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String[] args) {
(new Thread(new HelloRunnable())).start();
}
}
// 方法2
public class HelloThread extends Thread {
@Override
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String[] args) {
(new HelloThread()).start();
}
}