[py]c和java笔记语法对比

来源:互联网 发布:软件框架设计的艺术 编辑:程序博客网 时间:2024/05/23 16:56


c程序函数

http://c.biancheng.net/cpp/html/2903.html 

int main(){    int decks;    puts("请输入几幅牌: ");    scanf("%i", &decks);    if (decks < 1) {        puts ("无效的数");        return 1;    }    printf("一共有%i张牌\n", (decks * 52));    return 0;}


#include <stdio.h>int max(int a, int b){    if (a>b){        return a;    }else{        return b;    }}int main(){    int num1, num2, maxVal;    printf("Input two numbers: ");    scanf("%d %d", &num1, &num2);    maxVal = max(num1, num2);    printf("The max number: %d\n", maxVal);    return 0;}






java方法:

http://www.runoob.com/java/java-methods.html

public class TestMax{    public static void main(String[] args){        // System.out.println("hi this is a test")        int i=5;        int j=20;        int k=max(i,j);        System.out.println(k);    }    public static int max(int num1,int num2){        int result;        if (num1>num2)            result=num1;        else            result = num2;        return result;    }}

public class FinalizationDemo {    public static void main(String[] args) {      Cake c1 = new Cake(1);      Cake c2 = new Cake(2);      Cake c3 = new Cake(3);            c2 = c3 = null;      System.gc();  }  }   class Cake extends Object {    private int id;    public Cake(int id) {      this.id = id;      System.out.println("Cake Object " + id + "is created");    }        protected void finalize() throws java.lang.Throwable {      super.finalize();      System.out.println("Cake Object " + id + "is disposed");    }  }
Java中的命名规则


  包的命名 
  Java包的名字都是由小写单词组成。但是由于Java面向对象编程的特性,每一名Java程序员都可以编写属于自己的Java包,为了保障每个Java包命名的唯一性,在最新的Java编程规范中,要求程序员在自己定义的包的名称之前加上唯一的前缀。由于互联网上的域名称是不会重复的,所以程序员一般采用自己在互联网上的域名称作为自己程序包的唯一前缀。 
  例如: net.frontfree.javagroup
类的命名
   类的名字必须由大写字母开头而单词中的其他字母均为小写;如果类名称由多个单词组成,则每个单词的首字母均应为大写例如TestPage;如果类名称中包含单词缩写,则这个所写词的每个字母均应大写,如:XMLExample,还有一点命名技巧就是由于类是设计用来代表对象的,所以在命名类时应尽量选择名词。  
  例如: Circle
接口的命名
接口(Interfaces) 命名规则:大小写规则与类名相似 interface RasterDelegate;
interface Storing;
方法的命名
方法的名字的第一个单词应以小写字母作为开头,后面的单词则用大写字母开头。
  例如: sendMessge
变量的命名
变量(Variables) 除了变量名外,所有实例,包括类,类常量,均采用大小写混合的方式,第一个单词的首字母小写,其后单词的首字母大写。变量名不应以下划线或美元符号开头,尽管这在语法上是允许的。 
变量名应简短且富于描述。变量名的选用应该易于记忆,即,能够指出其用途。尽量避免单个字符的变量名,除非是一次性的临时变量。临时变量通常被取名为i,j,k,m和n,它们一般用于整型;c,d,e,它们一般用于字符型。 char c;
int i;
float myWidth;
实例变量(Instance Variables) 大小写规则和变量名相似,除了前面需要一个下划线 int _employeeId;
String _name;
Customer _customer;
常量的命名
常量的名字应该都使用大写字母,并且指出该常量完整含义。如果一个常量名称由多个单词组成,则应该用下划线来分割这些单词。
  例如: MAX_VALUE

原创粉丝点击