java 中void的使用

void是无返回值的意思。

比方说 public void walk()
      { 
            System.out.print("你好!");
      }
如果别的地方调用这个方法 就会打印出  你好!

再看这里:
      public String walk()     //注意这里的返回类型是String不是void了
      { 
            System.out.print("你好!");
         return "哈喽";       //注意这里这个return 如果不写就会报错!
      }
原因就是因为上面 的返回类型必须是String型
如果写的是void意思就是不需要返回类型。所以就没必要写return 了 
如果方法上面写了某种返回类型 就必须写return后面跟 相应的类型值
展开阅读全文

void *和void**

05-15

[code=C/C++]rn#includern#includern#includernrn#define DEBUGrn#define BOOL intrn#define MAX_STACK_SIZE 100rn#define TRUE 1rn#define FALSE 0rnrntypedef structrnrn void *value;rnsnode_t;rnrntypedef structrnrn snode_t base[MAX_STACK_SIZE];rn int top;rnstack_t;rnrnBOOL InitStack(void **pstack)rnrn stack_t *stack;rnrn if (NULL == pstack)rn return FALSE;rnrn stack = (stack_t*)malloc(sizeof(stack_t));rn if(NULL == stack)rn return FALSE;rn memset(stack,0,sizeof(stack_t));rn *pstack = stack;rn return TRUE;rnrnrnvoid DestroyStack(void **pstack, void(*snode_free)())rnrn stack_t *stack;rn int i;rn if(NULL == pstack || NULL == *pstack)rn return;rn stack = (stack_t*)*pstack;rn for(i = 0; i < MAX_STACK_SIZE; ++i)rn rn if(stack->base[i].value)rn rn (*snode_free)(stack->base[i].value);rn stack->base[i].value = NULL;rn rn rn free(stack);rn *pstack = NULL;rnrnrnvoid ClearStack(void **pstack, void (*snode_free)())rnrn stack_t *stack = (stack_t*)pstack;rn int i;rn if(NULL == stack)rn return;rn for(i = 0; i < MAX_STACK_SIZE; ++i)rn rn if(stack->base[i].value)rn rn (*snode_free)(stack->base[i].value);rn stack->base[i].value = NULL;rn rn rn stack->top = 0;rnrnrnBOOL StackEmpty(void *pstack)rnrn stack_t *stack = (stack_t*)pstack;rnrn if(NULL == stack)return TRUE;rn if(0 == stack->top)return TRUE;rnrn return FALSE;rnrnrnint StackLength(void *pstack)rnrn stack_t *stack = (stack_t*)pstack;rn if(NULL == stack) return -1;rnrn return stack->top;rnrnrnBOOL PushStack(void *pstack, void *value)rnrn stack_t *stack = (stack_t*)pstack;rn if(NULL == stack || NULL == value)return FALSE;rn if(MAX_STACK_SIZE == stack->top) return FALSE;rn stack->base[stack->top++].value = value;rn return TRUE;rnrnrnBOOL PopStack(void *pstack, void *pvalue)rnrn stack_t *stack = (stack_t*)pstack;rnrn if(NULL == stack || NULL == pvalue)return FALSE;rn if(0 == stack->top) return FALSE;rn *((void **)pvalue) = stack->base[--(stack->top)].value;rn stack->base[stack->top].value = NULL;rn return TRUE;rnrnrn#ifdef DEBUGrnrnint main(int argc,char *argv[])rnrn int i, *p, err = 0;rn void *s;rn if(FALSE == InitStack(&s))rn rn printf("InitStack Failed\n");rn return -1;rn rn for(i = 0; i < 10; ++i)rn rn p = (int *)malloc(sizeof(int));rn *p = i;rn if(FALSE == PushStack(s,(void*)p))rn rn printf("PushStack Failed\n");rn err++;rn break;rn rn rn if(err == 0)rn rn while(TRUE == PopStack(s,(void*)&p))rn rn printf("get a ele from stack:%d\n", *p);rn free(p);rn rn rn DestroyStack(&s,free);rn return 0;rnrn#endifrn[/code]rn这是一个堆栈的数组实现。rn初始化的时候为什么不是传void *,而是传了void **rnvoid*和void **到底怎么用?rnvoid ClearStack(void **pstack, void (*snode_free)());这里也不是很懂。rn虚心求教。 论坛

没有更多推荐了,返回首页